我的 Java 代码中的 Netbeans 出现此错误:
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);
}
我不知道我的错误是什么,我在互联网上搜索过类似的错误,但我读过的任何解决方案都没有奏效(原谅我的英语不好)。
非常感谢!