Should Hibernate manage database design?

I spent all yesterday reading various articles / tutorials on Hibernate, and although I am excited about how strong he is, I have one serious problem with him.

It seems like standard practice is to let Hibernate create / generate your DB schema for you, which is a new and scary concept that I'm suffocating. From the tutorials I read, you simply add a new object to your hibernate.cfg.xml configuration file, comment on any POJO you want with @Entity , and voila - Hibernate creates the tables for you. Although this is very cool, I'm curious about a few scenarios:

  • What if you already have a database schema, and the one that Hibernate wants to generate for you does not match it? What if you have a crazy database administrator who refuses to budge according to a predefined (non-hibernate) scheme?
  • What if you have lookup tables with tens of thousands of entries in it (as in all cities in the world)? Do I need to create tens and thousands of unique POJOs and save() or is there a way to configure Hibernate to check and not overwrite data that already exists in your tables?
  • What if you want to perform customization on your schema or tables? Does this include indexing normalizing above and above what Hibernate automatically creates?
  • What if you want to add constraints or triggers to your tables? Indices?

I assume this is fundamentally the following:

It seems that Hibernate creates and forces the specific schema / configuration of your database. I wonder how this agenda will run counter to our platform standards, our DBA philosophies, and our ability to tune / tune the tables with which Hibernate interacts.

Thanks in advance.

+6
source share
9 answers

Hibernate comes with a standard way to map objects to tables - for example, with several tools / libraries, which simplifies configuration settings for simplification.

However, if you want to map entities to database tables in different ways, you can explicitly tell Hibernate how they are displayed (from simple attributes, such as changing the table name, to overriding foreign key relationships between related objects and methods, this is preserved).

If you do it right, you don’t need to create and save existing data, because it would be pointless - the database already contains information about objects in exactly the form that Hibernate understands. (Think about it - to load and then immediately save the object, there should always be no-op, and therefore you can skip it altogether.)

So, the short answer to your question is no. If you don't care about creating tables, you can let Hibernate accept a reasonable default. If you want to explicitly configure your circuit, you can do it and then describe this exact circuit for Hibernate.

+2
source

I think that you attribute too much power to Hibernate.

Hibernate has an idiom that can affect database implementation.

Hibernation does not generate a circuit for you unless you ask it to do so. You can start with an existing schema and map it to Java objects using Hibernate. But this may not be possible or optimal if the circuit conflicts with Hibernate requirements.

If the database administrator does not budge - as they should not - or Hibernate cannot place you, then you have the answer: you cannot use Hibernate.

Your database administrator may agree, but your application may find that the dynamic SQL generated by Hibernate for you is not what you want.

Fortunately for you, this is not the only game in the city.

I do not think that the implementation should be completely or nothing. If you use simple JDBC to access reference data, what harm?

Database design considerations must be independent of Hibernate. Constraints, triggers, normalization, and indexes should be determined by the needs of the business, not the choice of middleware.

If you don’t have a solid object model or the circuit cannot accommodate it, you should reconsider Hibernate. Alternatively, there are direct JDBC, stored procedures, Spring JDBC and iBatis.

+4
source

Not. You can use hibernate tools to create entities from an existing database.

There are two ways to use Hibernate. If you have a good database or database administrator, then it is best to create a database and then put it to sleep. On the other hand, if you do not have a DBA and you have a good developer, let Hibernate create a database for you.

The concept of Hibernate is the mapping of databases and objects. Therefore, it is called an ORM (Object-Relational Mapping) tool.
Read here for the relational impedance of objects.

+1
source

As a person who worked for a long time on java and sleeping in the enterprise, I saw very few projects that use this opportunity. You will see some build tools and other things, but for a real enterprise application, I have never seen this.

Most database administrators will not allow the application user to create tables. They rely on the privileged user to perform these actions, and the user to whom the application connects, as well as r / w-privates by data, but not by the scheme itself.

As a result, you write the SQL yourself and you map the sleep mode. This does not mean that your object design will not affect your SQL, but you should always create your schema up anyway.

+1
source

This is the preferred method for a quick prototype or simple tutorial, but it is not the preferred method for any production application. In many ways, I prefer to create a database myself, using scripts to generate schemas, tables, views, indexes, etc. And map the circuit to the objects.

As long as the mapping finds tables and columns in the database, everything is in order.

Once you have the data in your database and the schema needs to change, you still have to write migration scripts. You cannot just drop everything and restart from scratch. Tutorials are written for developers starting with Hibernate and who should quickly discover Hibernate without dealing with complex SQL scripts.

0
source
  • What if you already have a DB schema ...

I don’t know how you got that impression. Hibernate may use an existing schema. It is quite flexible.

  • What if you have lookup tables ...

Make a LAZY connection and it will not load automatically. Only the changed object is saved.

  • What if you want to perform a performance ...

Just do not use the generated circuit. This is just a starting point. You can customize as you need.

  • What if you want to add constraints or triggers to your tables? Indices?

Some as above.

0
source

Hibernate can be used with an existing database schema. You can use various annotations to display existing tables and columns, for example:

@Table (name = "dbschema.dbTable") - must be placed in front of your class file to match it @Column (name = "colName") - display the column

Just make sure hibernate is configured with this option: hibernate.hbm2ddl.auto = update

If you set this to create, it will create the circuit, so don't do it in your case.

0
source

Use hibernate / jpa whenever possible. A common practice when developing applications is to extract a draft and manually modify it after queries (indexes, etc.). However, it will hurt you if you change the db layout with hibernate to do something. A lot of JPA beauty will be lost. For tasks that require a lot of performance tuning and full control, just go to reguar jdbc.

0
source

Some answers:
A. You can add an index annotation: see table annotation.

B. If you have lookup tables, you can choose the lazy selection or the desired selection (i.e. if your tables represent the person and his books - should the person load without his book or with his books)

C. Sleep mode can be used to work on an existing circuit. The scheme may not be trivial to work, but as others have said, you should design db only in accordance with the needs of the business, and not in accordance with structure agreements

D. I would also urge you to read what hibernate does under the hood - it uses a large number of proxies, which degrades performance, you must understand the session volume and the use of the 1st level and 2nd level cache

. E. Following what I wrote in section D, working with triggers will cause your database to change “under the hood” when it comes to sleep mode. Consider the case when updating a record will create (using a trigger) a record in some archiving table, and let this table also be annotated with hibernate
- Your sleep caching will not be aware of changes that have occurred outside the scope.

F. It is important for me to state that I am not against Hibernate, but you should not use it for all decisions, this is a mistake I made in the past. Now I am working with Spring -JDBC, and I am very satisfied (for our application it will be difficult to use Hibernate, and I assume that we will consider this only when we need to support more than one DB taste).

0
source

Source: https://habr.com/ru/post/918679/


All Articles