【问题标题】:Why is hbm.xml configuration is followed by hibernate when I use both hbm.xml and annotation to configure the same data?为什么我同时使用hbm.xml和注解来配置相同的数据时,hbm.xml配置后跟hibernate?
【发布时间】:2014-05-18 18:41:00
【问题描述】:

提前非常感谢。 :) 我正在使用休眠 4.3.5。我写了一个休眠应用程序。我在 hbm.xml 文件中配置了表名和其中一列的名称以及使用注释。当我运行应用程序时,创建的表在 hbm.xml 文件中设置了属性。在我看来,它应该使用注解,这可能是为了向后兼容,但是如果我有一个具有 hbm 文件的旧版休眠应用程序,现在我决定使用注解覆盖现有的 hbm.xml 配置怎么办? 这是我的代码。 我的实体类

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;
}


}

我的用户.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>

我的申请文件

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();
    }

}

最后是输出(生成的查询)

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

【问题讨论】:

  • 你看过类似的问题吗?这个答案看起来很适合你:stackoverflow.com/a/8643515/3402809
  • 是的,我已经访问过该链接,但没有说明为什么在重复配置的情况下优先考虑 hbm 配置?谢谢:)

标签: xml hibernate configuration hbm


【解决方案1】:

代替

配置配置=new Configuration();

使用

配置配置 = new AnnotationConfiguration()

这将优先考虑注释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 2011-02-20
    • 1970-01-01
    • 2012-11-19
    • 2012-05-07
    • 1970-01-01
    相关资源
    最近更新 更多