Java Hibernate一对一映射问题

Java Hibernate一对一映射问题,java,mysql,hibernate,Java,Mysql,Hibernate,你好,我有以下课程: 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

你好,我有以下课程:

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带来任何问题,因为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>
正如您所看到的,它说的是“模式生成完成”(我正在使用updatefordevelopment)

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


我发现了几个问题:

  • 异常是因为类
    Materiali
    的使用中存在错误(它还有一个字段
    movimenti
    ,该字段必须为非空,但与您的问题无关,您会被它弄糊涂)

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

  • 我真的建议对映射使用注释。它们更易于使用,并将所有信息保存在一个地方

  • 在这两个表中看不到任何引用的原因是Hibernate创建了多对多映射(这需要第三个表)。我理解你想要实现的目标,但我不确定Hibernate是否足够聪明


  • 我发现了几个问题:

  • 异常是因为类
    Materiali
    的使用中存在错误(它还有一个字段
    movimenti
    ,该字段必须为非空,但与您的问题无关,您会被它弄糊涂)

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

  • 我真的建议对映射使用注释。它们更易于使用,并将所有信息保存在一个地方

  • 在这两个表中看不到任何引用的原因是Hibernate创建了多对多映射(这需要第三个表)。我理解你想要实现的目标,但我不确定Hibernate是否足够聪明

  • 这个(目前只有)答案怎么可能有“-1”票?这个(目前只有)答案怎么可能有“-1”票??
        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
    
    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