Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong ordering in generated table in jpa

This (should) be a rather simple thing to do, however I am struggling.

I want a table to be generated like this:

 id  organizationNumber  name 

However, when I look in the database, I see that the ordering is wrong. Does anybody know how I can force hibernate/jpa to generate the table with correct ordering?

 desc Organization; +--------------------+--------------+------+-----+---------+----------------+ | Field              | Type         | Null | Key | Default | Extra          | +--------------------+--------------+------+-----+---------+----------------+ | id                 | bigint(20)   | NO   | PRI | NULL    | auto_increment |  | name               | varchar(255) | NO   |     | NULL    |                |  | organizationNumber | varchar(255) | NO   | UNI | NULL    |                |  +--------------------+--------------+------+-----+---------+----------------+ 

This is how my entity bean looks like:

@Entity @NamedQuery(name = "allOrganizations", query = "SELECT org FROM Organization org order by name") public class Organization {      private Long id;     private String organizationNumber;     private String name;      public Organization() {     }      public Organization(String name) {         this.name = name;     }      @Id     @GeneratedValue     public Long getId() {         return id;     }      @SuppressWarnings("unused")     private void setId(Long id) {         this.id = id;     }      @NotEmpty     @Column(unique=true, nullable=false)     public String getOrganizationNumber() {         return organizationNumber;     }        public void setOrganizationNumber(String organizationNumber) {         this.organizationNumber = organizationNumber;     }       @NotEmpty     @Column(nullable=false)     public String getName() {         return name;     }      public void setName(String name) {         this.name = name;     }      @Override     public String toString() {         return this.name + " " + this.organizationNumber;     } } 
like image 588
Shervin Asgari Avatar asked Aug 19 '09 07:08

Shervin Asgari


People also ask

Is @column mandatory in JPA?

@Column. Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.

Which method in JPA is used for updating data in a table?

JPA and Hibernate provide different methods to persist new and to update existing entities. You can choose between JPA's persist and merge and Hibernate's save and update methods.


2 Answers

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).

like image 186
cletus Avatar answered Oct 04 '22 04:10

cletus


DataNucleus allows the extension specifying the position for schema generation, FWIW.

like image 40
DataNucleus Avatar answered Oct 04 '22 04:10

DataNucleus