Why is hbm.xml configuration is followed by hibernate when I use both hbm.xml and annotation to configure the same data?

StackOverflow https://stackoverflow.com/questions/22896873

Question

Thanks a lot in advance. :) I am using Hibernate 4.3.5. I wrote a Hibernate application. I configured table name and name of one of it's columns,in hbm.xml file as well as using annotation. When I ran the application the table created was having the properties set in hbm.xml file. In my opinion it should have used annotation instead, it may be for backward compatibility but what if I have a legacy hibernate app having hbm files and now I decide to use annotations to override the existing hbm.xml configuration? Here is my code. My entity class

package com.infinite.entity;

import javax.persistence.Column;
import javax.persistence.Table;

import org.hibernate.annotations.Entity;

@javax.persistence.Entity
@Table(name="ANNOTATION_USER")
public class User {
private int userId;

@Column(name="ANNOTATION_FIRSTNAME")
private String firstName;
String lastName, city, country;
public int getUserId() {
    return userId;
}
public void setUserId(int userId) {
    this.userId = userId;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}


}

My User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 27 Feb, 2014 8:38:13 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.infinite.entity.User" table="HBM_USER">
        <id name="userId" type="int">
            <column name="USERID" />
            <generator class="native" />
        </id>
        <property name="firstName" type="java.lang.String">
            <column name="HBM_FIRSTNAME" />
        </property>
        <property name="lastName" type="java.lang.String">
            <column name="LASTNAME" />
        </property>
        <property name="city" type="java.lang.String">
            <column name="CITY" />
        </property>
        <property name="country" type="java.lang.String">
            <column name="COUNTRY" />
        </property>
    </class>
</hibernate-mapping>

My application file

package com.infinite.entity;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test {

    public static void main(String[] args) {

        User user= new User();
        user.setCity("Dewas");
        user.setCountry("india");
        user.setFirstName("SGN");
        user.setLastName("Jai ho");
        user.setUserId(100);
        // TODO Auto-generated method stub
Configuration configuration=new Configuration();
configuration=configuration.configure();
SessionFactory factory=configuration.buildSessionFactory();
Session session=factory.openSession();
Transaction transaction= session.beginTransaction();
session.save(user);
//user.setCountry("Australia");

transaction.commit();
session.close();
    }

}

and finally the output (query generated)

Hibernate: 
    insert 
    into
        HBM_USER
        (HBM_FIRSTNAME, LASTNAME, CITY, COUNTRY) 
    values
        (?, ?, ?, ?)
Was it helpful?

Solution

Instead of

Configuration configuration=new Configuration();

use

Configuration configuration = new AnnotationConfiguration()

This will give preference to annotations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top