休眠自动创建重复列
hibernate creating duplicate columns automatically
我最近开始学习 HQL 并尝试创建一个测试应用程序以查看它是如何工作的..但是当我尝试 运行 该项目时,我看到正在创建具有 NULL 值的重复列在里面。
这是我的项目详情
我有一个 table(保险),结构是我创建的 {id,insurance_name,invested_amount,investment_date}。
当我 运行 应用程序时,它正在创建两个具有空值的附加列 {id,insurance_name,invested_amount,investment_date,INSURANCE_AMOUNT ,INSURANCE_DATE } 而且我不知道它们是从哪里创建的。
休眠配置文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate4</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="insurance.hbm.xml"/>
</session-factory>
映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.roseindia.app.HQL.Insurance" table="INSURANCE">
<id name="id" type="long" column="ID">
<generator class="increment"></generator>
</id>
<property name="insuranceName">
<column name="INSURANCE_NAME"></column>
</property>
<property name="investmentAmount">
<column name="INSURANCE_AMOUNT"></column>
</property>
<property name="investmentDate">
<column name="INSURANCE_DATE"></column>
</property>
</class>
</hibernate-mapping>
POJO class:
public class Insurance {
private Long id;
private String insuranceName;
private Integer investmentAmount;
private Date investmentDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getInsuranceName() {
return insuranceName;
}
public void setInsuranceName(String insuranceName) {
this.insuranceName = insuranceName;
}
public Integer getInvestmentAmount() {
return investmentAmount;
}
public void setInvestmentAmount(Integer investmentAmount) {
this.investmentAmount = investmentAmount;
}
public Date getInvestmentDate() {
return investmentDate;
}
public void setInvestmentDate(Date investmentDate) {
this.investmentDate = investmentDate;
}
}
主要class:
public class SelectClauseExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session session =null;
try{
SessionFactory sessionFactory =new Configuration().configure().buildSessionFactory();
session= sessionFactory.openSession();
String SQL_QUERY="select insurance.id, insurance.insuranceName,insurance.investmentAmount,insurance.investmentDate from Insurance insurance";
Query query = session.createQuery(SQL_QUERY);
for (java.util.Iterator it = query.iterate(); it.hasNext();){
System.out.print("Inside for");
Object[] row = (Object[]) it.next();
System.out.println("ID: "+row[0]);
System.out.println("Name: "+row[1]);
System.out.println("Amount: "+row[2]);
System.out.println("Date: "+row[3]);
}
}catch(Exception e){
}finally{
session.flush();
session.close();
}
}
}
你应该从你的配置文件中跳过 属性 hibernate.hbm2ddl.auto
(这意味着默认值,这意味着当省略此设置时不会发生验证、更新、创建或删除)或使用 validate
如果您不想在您的数据库中进行任何更新
请参阅 Hibernate hbm2ddl.auto, possible values and what they do - any official explanation? 以了解 'hibernate.hbm2ddl.auto'
的可能值
尝试替换这个:
<property name="hibernate.hbm2ddl.auto">update</property>
与:
<property name="hibernate.hbm2ddl.auto">validate</property>
在此模式下,您的架构不会有任何变化,因为您的文件中有 Mapping file:
<property name="investmentAmount">
<column name="INSURANCE_AMOUNT"></column>
</property>
<property name="investmentDate">
<column name="INSURANCE_DATE"></column>
每次 运行 您的应用程序 yoyr 架构都会更改。
我最近开始学习 HQL 并尝试创建一个测试应用程序以查看它是如何工作的..但是当我尝试 运行 该项目时,我看到正在创建具有 NULL 值的重复列在里面。
这是我的项目详情
我有一个 table(保险),结构是我创建的 {id,insurance_name,invested_amount,investment_date}。
当我 运行 应用程序时,它正在创建两个具有空值的附加列 {id,insurance_name,invested_amount,investment_date,INSURANCE_AMOUNT ,INSURANCE_DATE } 而且我不知道它们是从哪里创建的。
休眠配置文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate4</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="insurance.hbm.xml"/>
</session-factory>
映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.roseindia.app.HQL.Insurance" table="INSURANCE">
<id name="id" type="long" column="ID">
<generator class="increment"></generator>
</id>
<property name="insuranceName">
<column name="INSURANCE_NAME"></column>
</property>
<property name="investmentAmount">
<column name="INSURANCE_AMOUNT"></column>
</property>
<property name="investmentDate">
<column name="INSURANCE_DATE"></column>
</property>
</class>
</hibernate-mapping>
POJO class:
public class Insurance {
private Long id;
private String insuranceName;
private Integer investmentAmount;
private Date investmentDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getInsuranceName() {
return insuranceName;
}
public void setInsuranceName(String insuranceName) {
this.insuranceName = insuranceName;
}
public Integer getInvestmentAmount() {
return investmentAmount;
}
public void setInvestmentAmount(Integer investmentAmount) {
this.investmentAmount = investmentAmount;
}
public Date getInvestmentDate() {
return investmentDate;
}
public void setInvestmentDate(Date investmentDate) {
this.investmentDate = investmentDate;
}
}
主要class:
public class SelectClauseExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session session =null;
try{
SessionFactory sessionFactory =new Configuration().configure().buildSessionFactory();
session= sessionFactory.openSession();
String SQL_QUERY="select insurance.id, insurance.insuranceName,insurance.investmentAmount,insurance.investmentDate from Insurance insurance";
Query query = session.createQuery(SQL_QUERY);
for (java.util.Iterator it = query.iterate(); it.hasNext();){
System.out.print("Inside for");
Object[] row = (Object[]) it.next();
System.out.println("ID: "+row[0]);
System.out.println("Name: "+row[1]);
System.out.println("Amount: "+row[2]);
System.out.println("Date: "+row[3]);
}
}catch(Exception e){
}finally{
session.flush();
session.close();
}
}
}
你应该从你的配置文件中跳过 属性 hibernate.hbm2ddl.auto
(这意味着默认值,这意味着当省略此设置时不会发生验证、更新、创建或删除)或使用 validate
如果您不想在您的数据库中进行任何更新
请参阅 Hibernate hbm2ddl.auto, possible values and what they do - any official explanation? 以了解 'hibernate.hbm2ddl.auto'
的可能值尝试替换这个:
<property name="hibernate.hbm2ddl.auto">update</property>
与:
<property name="hibernate.hbm2ddl.auto">validate</property>
在此模式下,您的架构不会有任何变化,因为您的文件中有 Mapping file:
<property name="investmentAmount">
<column name="INSURANCE_AMOUNT"></column>
</property>
<property name="investmentDate">
<column name="INSURANCE_DATE"></column>
每次 运行 您的应用程序 yoyr 架构都会更改。