您好我有以下课程:
public class Movimenti implements java.io.Serializable {
private Integer id = null;
private Integer idCommessa = null;
private String nomemovimento = null;
private Movimento preventivato = null;
private Movimento effettivo = null;
public Movimento getEffettivo() {
return effettivo;
}
public void setEffettivo(Movimento effettivo) {
this.effettivo = effettivo;
}
public Movimento getPreventivato() {
return preventivato;
}
public void setPreventivato(Movimento preventivato) {
this.preventivato = preventivato;
}
public Movimenti() {
}
public Movimenti(Integer idCommessa, String nomemovimento) {
this.idCommessa = idCommessa;
this.nomemovimento = nomemovimento;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getIdCommessa() {
return this.idCommessa;
}
public void setIdCommessa(Integer idCommessa) {
this.idCommessa = idCommessa;
}
public String getNomemovimento() {
return this.nomemovimento;
}
public void setNomemovimento(String nomemovimento) {
this.nomemovimento = nomemovimento;
}
}
正如您所看到的,有两个Movimento参考。 Movimento看起来像这样:
public class Movimento {
Integer id = null;
Movimenti movimenti;
String descrizione = null;
public Movimenti getMovimenti() {
return movimenti;
}
public void setMovimenti(Movimenti movimenti) {
this.movimenti = movimenti;
}
public String getDescrizione() {
return descrizione;
}
public void setDescrizione(String descrizione) {
this.descrizione = descrizione;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
在movimento中提到了Movimenti。现在movimento是其他类的基类,并且没有问题,因为SchemaExport正确地完成了他的工作。我想要的是在Movimento和Movimenti之间建立一对一的关系,这样当我加载Movimenti实例时我可以自动回顾Movimento,反之亦然,并自动删除孤立的Movimento对象。因此,我想到将Movimenti中的两个一对一关系转向Movimento,一个倒退。但它不起作用,它不会生成正确的数据库表,甚至会发出异常。这些是映射(甚至还有我不包括的子类)。
Movimento.hbm.xml:
<?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 dynamic-insert="false" dynamic-update="false" mutable="true" name="persistence.beans.jCommesse.Movimento" optimistic-lock="version" polymorphism="implicit" select-before-update="false">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name = "descrizione" type="java.lang.String">
<column name = "descrizione"></column>
</property>
<one-to-one name = "movimenti" class = "persistence.beans.jCommesse.Movimento" constrained="true">
</one-to-one>
<joined-subclass name = "persistence.beans.jCommesse.Materiali" table = "Materiali">
<key column="id"/>
<property name = "consegnato" type="java.lang.Boolean">
<column name = "consegnato"/>
</property>
<property name="costo" type = "java.lang.Double">
<column name = "costo"/>
</property>
<property name = "costoTrasporto" type = "java.lang.Double">
<column name = "costoTrasporto"/>
</property>
<property name = "quantita" type = "java.lang.Double">
<column name = "quantita"/>
</property>
<property name = "riferimentoFattura" type = "java.lang.Integer">
<column name = "riferimentoFattura"/>
</property>
<property name = "tipoQuantita" type = "java.lang.String">
<column name = "tipoQuantita"/>
</property>
</joined-subclass>
<joined-subclass name = "persistence.beans.jCommesse.RientroMateriali" table = "RientroMateriali">
<key column="id"/>
<property name = "costo" type = "java.lang.Double">
<column name = "costo"/>
</property>
<property name = "costoDelTrasporto" type = "java.lang.Double">
<column name = "costoDelTrasporto"/>
</property>
<property name = "quantita" type = "java.lang.Double">
<column name = "quantita"/>
</property>
<property name = "riferimentoFattura" type = "java.lang.Integer">
<column name = "riferimentoFattura"/>
</property>
<property name = "tipoQuantita" type = "java.lang.String">
<column name = "tipoQuantita"/>
</property>
</joined-subclass>
<joined-subclass name = "persistence.beans.jCommesse.CostiExtra" table = "CostiExtra">
<key column = "id"/>
<property name = "nota" type = "java.lang.String">
<column name = "nota"/>
</property>
<property name = "prezzo" type = "java.lang.Double">
<column name = "prezzo"/>
</property>
</joined-subclass>
</class>
</hibernate-mapping>
和Movimenti.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 10-feb-2010 11.24.48 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="persistence.beans.jCommesse.Movimenti" table="movimenti" catalog="jcommesse">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="idCommessa" type="java.lang.Integer">
<column name="idCommessa" />
</property>
<property name="nomemovimento" type="string">
<column name="nomemovimento" length="250" />
</property>
<one-to-one name="preventivato" class="persistence.beans.jCommesse.Movimento" cascade="all" />
</class>
</hibernate-mapping>
所有这些都没有创建表格,而是出现了这个令人讨厌的例外:
10-feb-2010 15.04.46 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: schema update complete
Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: persistence.beans.jCommesse.Materiali.movimenti
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
at testgeneration.testSchema(testgeneration.java:34)
at testgeneration.main(testgeneration.java:53)
Java Result: 1
正如您所看到的那样,“架构生成完成”(我正在使用更新进行开发)。
Movimenti和Movimento在mySQL上出现如下:
CREATE TABLE `movimenti` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`idCommessa` int(11) DEFAULT NULL,
`nomemovimento` varchar(250) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
和
CREATE TABLE `movimento` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`descrizione` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FKBEB397FC4778E205` (`id`),
CONSTRAINT `FKBEB397FC4778E205` FOREIGN KEY (`id`) REFERENCES `movimento` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1
答案 0 :(得分:1)
我发现了一些问题:
异常是因为使用了类Materiali
时出现了一个错误(它还有一个字段movimenti
必须是非空的,但这与您的问题无关感到困惑。
在地图Movimenti.hbm.xml
中,您忘记了两个字段preventivato
和effettivo
的映射。您必须使用reverse=true
。
我真的建议使用注释进行映射。它们使用起来要简单得多,并将所有信息保存在一个地方。
您在两个表中看不到任何引用的原因是因为Hibernate创建了多对多映射(需要第三个表)。我理解你想要实现的目标,但我不确定Hibernate是否足够聪明。