gpt4 book ai didi

java - Hibernate一对一映射问题

转载 作者:行者123 更新时间:2023-11-29 01:02:21 28 4
gpt4 key购买 nike

你好,我有以下类(class):

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

最佳答案

我发现了几个问题:

  1. 异常是因为类 Materiali 的使用存在错误(它还有一个必须为非空的字段 movimenti 但事实并非如此与你的问题相关,你对此感到困惑)。

  2. 在映射 Movimenti.hbm.xml 中,您忘记映射两个字段 preventivatoeffettivo。您必须使用 reverse=true 映射它们。

  3. 我真的建议为映射使用注释。它们使用起来要简单得多,并将所有信息保存在一个地方。

  4. 您在两个表中看不到任何引用的原因是因为 Hibernate 创建了一个多对多映射(这需要第三个表)。我了解您要实现的目标,但我不确定 Hibernate 是否足够智能以实现它。

关于java - Hibernate一对一映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2237461/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com