2013-04-12 48 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

相關問題