2013-04-12 47 views
1

我有我的Java代码在Netbeans的这个错误:org.hibernate.MappingException:外键(FK12A711396456CA10 :)必须有相同数量的列引用的主键

org.hibernate.MappingException: Foreign key (FK12A711396456CA10:devolucion_master [devolucion_consecutivo])) must have same number of columns as the referenced primary key (devolucion [detalle_ticket_id,detalle_ticket_ticket_id,detalle_ticket_fondo_fijo_id,detalle_ticket_caja_id,consecutivo] 

我制作的国外从DevolucionMaster到Devolucion的关键,使用Devolucion的“consecutivo”和DevolucionMaster的变量“consecutivo”,问题在于Devolucion的“键”是复合键,而我只用于外键一个键的元素,可能是这就是为什么(它需要使用5作为主键)。

这里的DevolucionMaster.hbm.mxl:

<hibernate-mapping> 
    <class catalog="pos" name="dunosusa.pos.model.DevolucionMaster" table="devolucion_master"> 
    <composite-id class="dunosusa.pos.model.DevolucionMasterId" name="id"> 
     <key-property name="id" type="int"> 
     <column name="id"/> 
     </key-property> 
     <key-property name="detalleTicketTicketId" type="int"> 
     <column name="detalle_ticket_ticket_id"/> 
     </key-property> 
     <key-property name="detalleTicketFondoFijoId" type="int"> 
     <column name="detalle_ticket_fondo_fijo_id"/> 
     </key-property> 
     <key-property name="detalleTicketCajaId" type="int"> 
     <column name="detalle_ticket_caja_id"/> 
     </key-property> 
    </composite-id> 
    <many-to-one class="dunosusa.pos.model.Devolucion" fetch="select" name ="devolucion"> 
     <column name="devolucion_consecutivo" not-null="true"/> 
    </many-to-one> 
    <many-to-one class="dunosusa.pos.model.Usuario" fetch="select" name="usuario"> 
     <column length="6" name="usuario_clave_autorizo"/> 
    </many-to-one> 
    <many-to-one class="dunosusa.pos.model.Ticket" fetch="select" insert="false" name="ticket" update="false"> 
     <column name="detalle_ticket_ticket_id" not-null="true"/> 
     <column name="detalle_ticket_fondo_fijo_id" not-null="true"/> 
     <column name="detalle_ticket_caja_id" not-null="true"/> 
    </many-to-one> 
    <property name="total" type="big_decimal"> 
     <column name="total" not-null="true" precision="10"/> 
    </property> 
    <property name="fecha" type="timestamp"> 
     <column length="19" name="fecha"/> 
    </property> 
    </class> 
</hibernate-mapping> 

这里Devolucion.hbm.xml:

<hibernate-mapping> 
    <class catalog="pos" name="dunosusa.pos.model.Devolucion" table="devolucion"> 
    <composite-id class="dunosusa.pos.model.DevolucionId" name="id"> 
     <key-property name="detalleTicketId" type="int"> 
     <column name="detalle_ticket_id"/> 
     </key-property> 
     <key-property name="detalleTicketTicketId" type="int"> 
     <column name="detalle_ticket_ticket_id"/> 
     </key-property> 
     <key-property name="detalleTicketFondoFijoId" type="int"> 
     <column name="detalle_ticket_fondo_fijo_id"/> 
     </key-property> 
     <key-property name="detalleTicketCajaId" type="int"> 
     <column name="detalle_ticket_caja_id"/> 
     </key-property> 
     <key-property name="consecutivo" type="int"> 
     <column name="consecutivo"/> 
     </key-property> 
    </composite-id> 
    <many-to-one class="dunosusa.pos.model.MotivoDevolucion" fetch="select" name="motivoDevolucion"> 
     <column name="motivo_devolucion_id" not-null="true"/> 
    </many-to-one> 
    <many-to-one class="dunosusa.pos.model.DetalleTicket" fetch="select" insert="false" name="detalleTicket" update="false"> 
     <column name="detalle_ticket_id" not-null="true"/> 
     <column name="detalle_ticket_ticket_id" not-null="true"/> 
     <column name="detalle_ticket_fondo_fijo_id" not-null="true"/> 
     <column name="detalle_ticket_caja_id" not-null="true"/> 
    </many-to-one> 
    <many-to-one class="dunosusa.pos.model.ControlCorte" fetch="select" name="controlCorte"> 
     <column name="control_corte_fondo_fijo_id" not-null="true"/> 
     <column name="control_corte_caja_id" not-null="true"/> 
    </many-to-one> 
    <property name="cantidad" type="big_decimal"> 
     <column name="cantidad" precision="8"/> 
    </property> 
    <property name="fecha" type="timestamp"> 
     <column length="19" name="fecha"/> 
    </property> 
    <property name="comentario" type="string"> 
     <column length="150" name="comentario"/> 
    </property> 
    <property name="controlDevolucion" type="boolean"> 
     <column name="control_devolucion" not-null="true"/> 
    </property> 
    <set name="devolucionMasters" inverse="true"> 
     <key> 
      <column name="devolucion_consecutivo" not-null="true" /> 
     </key> 
     <one-to-many class="dunosusa.pos.model.DevolucionMaster" /> 
    </set> 
    </class> 
</hibernate-mapping> 

DevolucionMaster.java:(只变量,而不是set和get)

public class DevolucionMaster implements java.io.Serializable { 

    private DevolucionMasterId id; 
    private Devolucion devolucion; 
    private Usuario usuario; 
    private Ticket ticket; 
    private BigDecimal total; 
    private Date fecha; 
} 

Devolucion.java:(相同DevolucionMaster)

public class Devolucion implements java.io.Serializable { 

    private DevolucionId id; 
    private MotivoDevolucion motivoDevolucion; 
    private DetalleTicket detalleTicket; 
    private ControlCorte controlCorte; 
    private BigDecimal cantidad; 
    private Date fecha; 
    private String comentario; 
    private boolean controlDevolucion; 
    private Set devolucionMasters = new HashSet(0); 
} 

我不知道我的错误是什么,我在互联网上搜索了类似的错误,但没有一个解决方案我读过已经工作(原谅我的坏英语)。

非常感谢!

回答

1

是的,您对Devolucion类复合主键,和你想只用一个简单的列/字段devolucion_master.devolucion_consecutivo引用它。

我不知道如何轻松地告诉Hibernate列实际上是dunosusa.pos.model.DevolucionId类型。

这就是为什么我从来没有使用复合主键。我总是有一个Longbigint)类型的主键,并强制在需要的地方使用外键组合。使用起来更容易,没有像你所遇到的问题。

附录: 我只是做了一些挖掘,实际上,还有一个办法做到这一点:-) 但正如我说这不是为使用单柱PK/FK键一样简单。

取而代之的是:

<hibernate-mapping> 
    <class catalog="pos" name="dunosusa.pos.model.DevolucionMaster" table="devolucion_master"> 
    ... 
    <many-to-one class="dunosusa.pos.model.Devolucion" fetch="select" name="devolucion"> 
     <column name="devolucion_consecutivo" not-null="true"/> 
    </many-to-one> 
    ... 

您应该列出在FK所有PK列是这样的:

<hibernate-mapping package="dunosusa.pos.model"> 
    <class catalog="pos" name="DevolucionMaster" table="devolucion_master"> 
    ... 
    <many-to-one class="Devolucion" fetch="select" name="devolucion"> 
     <column name="devolucion_detalle_ticket_id" not-null="true"/> 
     <column name="devolucion_detalle_ticket_ticket_id" not-null="true"/> 
     <column name="devolucion_detalle_ticket_fondo_fijo_id" not-null="true"/> 
     <column name="devolucion_detalle_ticket_caja_id" not-null="true"/> 
     <column name="devolucion_consecutivo" not-null="true"/> 
    </many-to-one> 
    ... 

的一点小建议 - 所以你不使用hibernate-mapping元素的package属性不必在任何地方输入 - 这使得它更具可读性。

好运。

+0

那么,呃@Cebence,解决这个问题的最好方法是从'DevolucionMaster'中删除外键?并把它作为一个简单的列'devolucion_consecutivo'? – GrayFox

+0

但是在DB表'devolucion_master',我只检查了外键'devolucion'中的'consecutivo'列作为外键...如果我是对的,用你提供的解决方案,我将不得不标记或检查其他4个元素,这些元素使我的密钥为'devolution'表,对吧? – GrayFox

+0

是的,你需要在FK中有来自PK的所有列。您还可以在一张桌子上用一个例如仅使用3列FK的4列PK? (或者你的案例中有一列)。但是如果'devolucion.consecutivo'可以唯一地标识一行,那么你不需要'devolucion' PK中的所有其他列。 – Cebence

相关问题