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