Wrong Ordering in Generated Table in JPA

Wrong ordering in generated table in jpa

Hibernate generates columns in alphabetical order. According to this post the reason is given as:

It is sorted to ensurce deterministic
ordering across clusters.

We can't rely on the vm to return the
methods in the same order every time
so we had to do something.

Apparently it used to be in the order of occurrence but this changed between 3.2.0 GA and 3.2.1 GA.

I also found Schema auto generation creates columns in alphabetical order for compound primary keys and this seems to be like your problem. This ticket is about the order changing in primary keys and that negatively impacts index performance.

There is no fix for this other than a workaround of naming the columns in such a way that they come out in the correct order (no, I'm not kidding).

how to create table column in a particular order in hibernate?

As a matter of fact, the table created with the columns as specified by your annotations shouldn't be dependent on the sequence of it. Neither a SELECT * statement would entirely ensure the columns in a specific order as well.

And coming to a response from the Hibernate team, it looks like to be a Known issue and it is not possible to set the order of the columns through Hibernate. Post here.

To my understanding, we may not entirely rely on Hibernate's hbm2ddl on creating tables. The moment a table is created via Hibernate, we can very well ALTER it to order the columns as needed.

Hope this answers your question!

How to maintain the column order when creating a new table using hibernate?

Assuming you mean the database tables are generated via hbm2ddl.auto being set to CREATE or similar, there is no way to specify the order of the columns at least in 2008, according to one member of the Hibernate team.

My advice would be to create the database via separately maintained database scripts (possibly in conjunction with a script deployment/migration tool such as Flyway and perhaps also with hbm2ddl.auto = VALIDATE to check the resulting schema matches the entities). Maintaining the database via scripts becomes much more necessary once the application goes into production.

Hibernate Criteria query lists tables in wrong order in generated SQL

When criteria is used hibernate actually traverses entity tree in Depth First Search way to build joins according to fields definition from configuration. In your case BillEvent is traversed bill first, then subfields of Bill class. So basically it creates event entity join after it creates all joins from bill association. You can define the order in hbm.xml but as you mentioned it's not very scalable.

So you have at least two options here:

  1. Change criteria so root entity will be different, then add projection and result transformer to fetch BillEvent entities. For instance:

    final Criteria criteria = session.createCriteria(Event.class, "event1")
    .createCriteria("event1.billEvents", "be1")
    .createCriteria("be1.bill", "bill1")
    .createCriteria("bill1.billEvents", "be2")
    .createCriteria("be2.event", "event2", JoinType.LEFT_OUTER_JOIN,
    Restrictions.ltProperty("event1.time", "event2.time"))
    .add(Restrictions.eq("be1.id", billId))
    .add(Restrictions.isNull("event2.id"))
    .setProjection(Projections.projectionList()
    .add(Projections.property("be1.event"), "event")
    .add(Projections.property("be1.note"), "note"))
    .setResultTransformer(Transformers.aliasToBean(BillEvent.class));
  2. Another possible option is nto use hql rather then criteria api. It will allow you to control join order directly from request since it uses a different sql build mechanism.


Related Topics



Leave a reply



Submit