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


thanks lot in advance. :) using hibernate 4.3.5. wrote hibernate application. configured table name , name of 1 of it's columns,in hbm.xml file using annotation. when ran application table created having properties set in hbm.xml file. in opinion should have used annotation instead, may backward compatibility if have legacy hibernate app having hbm files , decide use annotations override existing hbm.xml configuration? here code. 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 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 output (query generated)

hibernate:      insert              hbm_user         (hbm_firstname, lastname, city, country)      values         (?, ?, ?, ?) 

instead of

configuration configuration=new configuration();

use

configuration configuration = new annotationconfiguration()

this give preference annotations.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -