2

I have used Hibernate to map Java objects to PostgreSQL database. UserDetails class is the entity class used to add user.It contains an embedded object called Address.

UserDetails.java

@Entity
@Table(name="USER_DETAILS")
public class UserDetails {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="USER_ID")
    private int userId;
    @Column(name="USER_NAME")
    private String userName;
    @Temporal(TemporalType.DATE)
    @Column(name="JOINED_DATE")
    private Date joinedDate;
    @Column(name="DESCRIPTION")
    private String description;
    @Embedded
    private Address address;
}

Address object contains detail about address like city,pincode,street and state.

Address.java

import javax.persistence.Embeddable;

@Embeddable
public class Address {
private String street;
private String city;
private String state;
private String pincode;
}

The class containing main method is below:

public class HibernateTest {
    public static void main(String[] args){
    UserDetails user = new UserDetails();
    UserDetails user1 = new UserDetails();

    user.setUserName("First User");

    Address newAddress = new Address();
    newAddress.setCity("Pune");
    newAddress.setPincode("411057");
    newAddress.setState("Maharashtra");
    newAddress.setStreet("Hinjewadi");

    user.setAddress(newAddress);
    user.setDescription("just like that");
    user.setJoinedDate(new Date());     
    user1.setUserName("Second User");
    user1.setDescription("great here");
    user1.setJoinedDate(new Date());

    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(user);
    session.save(user1);
    session.getTransaction().commit();
    session.close();

    user = null;

    session = sessionFactory.openSession();
    session.beginTransaction();
    user = session.get(UserDetails.class, 1);
    System.out.println(user.getUserName());
    session.close();
    sessionFactory.close();
}
}

The generated SQL query is as below, which shows that columns are not added in the same order in which they were declared.

Hibernate: create sequence hibernate_sequence start 1 increment 1
Hibernate: create table USER_DETAILS (USER_ID int4 not null, city varchar(255), pincode varchar(255), state varchar(255), street varchar(255), DESCRIPTION varchar(255), JOINED_DATE date, USER_NAME varchar(255), primary key (USER_ID))

Is there any rule in which columns are added in hibernate?

Tiny Jaguar
  • 433
  • 3
  • 12
  • 30

1 Answers1

1

The hbm to ddl library uses reflection to get the fields within a class and according to the documentation the order of the fields is not preserved / guaranteed:

https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getFields()

Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface has no accessible public fields, or if it represents an array class, a primitive type, or void.

I've experienced similar issues when it comes to writing libraries that rely on reflection.

StuPointerException
  • 7,117
  • 5
  • 29
  • 54
  • I have tried to recompile and execute again. It still adds columns in the same way. – Tiny Jaguar Feb 25 '16 at 17:48
  • 1
    The mysteries or reflection! "Not in a particular order" is not the same as "not in a consistent order". You just shouldn't rely on this ordering. – StuPointerException Feb 25 '16 at 17:49
  • Is there any way in Hibernate to add columns in a specific manner ? – Tiny Jaguar Feb 25 '16 at 17:50
  • Not as far as I know, it looks like a known limitation (https://forum.hibernate.org/viewtopic.php?p=2396344). I personally only use `hbm2ddl` to generate the initial schema and then modify it as required after that (there are usually a few things to fix!). – StuPointerException Feb 25 '16 at 17:52
  • does that mean that after using hbm2ddl, we need to sort the columns of table before importing any data into the table? – Tiny Jaguar Feb 25 '16 at 18:16