org.hibernate.MappingException:外键(FK12A711396456CA10 :)必须与引用的主键具有相同的列数 [英] org.hibernate.MappingException: Foreign key (FK12A711396456CA10:) must have same number of columns as the referenced primary key
问题描述
我有我的Java代码在Netbeans的这个错误:
org.hibernate.MappingException:外键(FK12A711396456CA10 :devolucion_master [devolucion_consecutivo]))必须具有相同数目的被引用的主密钥(列devolucion [detalle_ticket_id,detalle_ticket_ticket_id,detalle_ticket_fondo_fijo_id,detalle_ticket_caja_id,consecutivo]
'p>我做了一个外键从DevolucionMaster到Devolucion,使用consecutivo从Devolucion我的变量consecutivo为DevolucionMaster,问题是,Devolucion的钥匙是复合键,我只使用外键的一个关键元素,也许这就是为什么(它需要使用主键5)。
这是DevolucionMaster .hbm.mxl:
$ b
< hibernate-mapping>
< class catalog =posname = dunosusa.pos.model.DevolucionMastertable =devolucion_master>
< composite-id cl屁股=dunosusa.pos.model.DevolucionMasterIdname =id>
< key-property name =idtype =int>
< column name =id/>
< / key-property>
< key-property name =detalleTicketTicketIdtype =int>
< column name =detalle_ticket_ticket_id/>
< / key-property>
< key-property name =detalleTicketFondoFijoIdtype =int>
< column name =detalle_ticket_fondo_fijo_id/>
< / key-property>
< key-property name =detalleTicketCajaIdtype =int>
< column name =detalle_ticket_caja_id/>
< / key-property>
< / composite-id>
< column name =devolucion_consecutivonot-null =true/>
< /多对一>
< column length =6name =usuario_clave_autorizo/>
< /多对一>
< column name =detalle_ticket_ticket_idnot-null =true/>
< column name =detalle_ticket_fondo_fijo_idnot-null =true/>
< column name =detalle_ticket_caja_idnot-null =true/>
< /多对一>
< property name =totaltype =big_decimal>
< column name =totalnot-null =trueprecision =10/>
< / property>
< property name =fechatype =timestamp>
< column length =19name =fecha/>
< / property>
< / class>
< / hibernate-mapping>
这里是Devolucion.hbm.xml:
<休眠映射>
< class catalog =posname =dunosusa.pos.model.Devoluciontable =devolucion>
< composite-id class =dunosusa.pos.model.DevolucionIdname =id>
< key-property name =detalleTicketIdtype =int>
< column name =detalle_ticket_id/>
< / key-property>
< key-property name =detalleTicketTicketIdtype =int>
< column name =detalle_ticket_ticket_id/>
< / key-property>
< key-property name =detalleTicketFondoFijoIdtype =int>
< column name =detalle_ticket_fondo_fijo_id/>
< / key-property>
< key-property name =detalleTicketCajaIdtype =int>
< column name =detalle_ticket_caja_id/>
< / key-property>
< key-property name =consecutivotype =int>
< column name =consecutivo/>
< / key-property>
< / composite-id>
< column name =motivo_devolucion_idnot-null =true/>
< /多对一>
< column name =detalle_ticket_idnot-null =true/>
< column name =detalle_ticket_ticket_idnot-null =true/>
< column name =detalle_ticket_fondo_fijo_idnot-null =true/>
< column name =detalle_ticket_caja_idnot-null =true/>
< /多对一>
< column name =control_corte_fondo_fijo_idnot-null =true/>
< column name =control_corte_caja_idnot-null =true/>
< /多对一>
< property name =cantidadtype =big_decimal>
< column name =cantidadprecision =8/>
< / property>
< property name =fechatype =timestamp>
< column length =19name =fecha/>
< / property>
< property name =comentariotype =string>
< column length =150name =comentario/>
< / property>
< property name =controlDevoluciontype =boolean>
< column name =control_devolucionnot-null =true/>
< / property>
< set name =devolucionMastersinverse =true>
< key>
< column name =devolucion_consecutivonot-null =true/>
< / key>
< / set>
< / class>
< / hibernate-mapping>
DevolucionMaster.java :(只有变量,而不是set和get)
public class DevolucionMaster implements java.io.Serializable {
$ b $ private DevolucionMasterId id;
私人Devolucion devolucion;
private Usuario usuario;
私人机票;
私人BigDecimal总数;
私人日期范围;
$ b Devolucion.java:(和DevolucionMaster相同)
public class Devolucion implements java.io.Serializable {
private DevolucionId id;
private MotivoDevolucion motivoDevolucion;
私人DetalleTicket detalleTicket;
私有ControlCorte controlCorte;
private BigDecimal cantidad;
私人日期范围;
private String comentario;
私有布尔controlDevolucion;
private set devolucionMasters = new HashSet(0);
$ b我不知道我的错误是什么,类似的错误,但没有一个解决方案我读过已经工作(原谅我的英语不好)
非常感谢!
Devolucion 类,您有一个复合主键,并且您正尝试仅使用一个简单的列/字段 devolucion_master.devolucion_consecutivo
。
我不知道的方式轻松地告诉Hibernate是 devolucion_consecutivo
柱实际类型为 dunosusa.pos.model.DevolucionId
这就是为什么我从来没有使用复合主键。我总是有一个类型为 Long
( bigint
)的主键,并且在必要的地方为外键组合赋予唯一性。这样做比较容易,没有问题。
附录:
我只是做了一些挖掘和,实际上,有办法做到这一点:-)
但是正如我所说,它不像使用单列PK / FK键一样简单。
反而这个:
< hibernate-mapping>
< class catalog =posname =dunosusa.pos.model.DevolucionMastertable =devolucion_master>
...
< column name =devolucion_consecutivonot-null =true/>
< /多对一>
...
您应该列出FK中的所有PK列: p>
< hibernate-mapping package =dunosusa.pos.model>
< class catalog =posname =DevolucionMastertable =devolucion_master>
...
< column name =devolucion_detalle_ticket_idnot-null =true/>
< column name =devolucion_detalle_ticket_ticket_idnot-null =true/>
< column name =devolucion_detalle_ticket_fondo_fijo_idnot-null =true/>
< column name =devolucion_detalle_ticket_caja_idnot-null =true/>
< column name =devolucion_consecutivonot-null =true/>
< /多对一>
...
一点建议 - 使用 hibernate-mapping
元素的/ code>属性,所以你不必在任何地方输入它 - 这使得它更易读。
祝你好运。
I'm having this error at Netbeans in my Java code:
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]
I made a foreign key from DevolucionMaster to Devolucion, using "consecutivo" from Devolucion to my variable "consecutivo" for DevolucionMaster, the problem is that for Devolucion the "key" is composite key, and I only use for the foreign key one element of the key, maybe that's why (that it needs to be used the 5 that makes the primary key).
Here's the 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>
here the 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: (only the variables, not the set and 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: (same as for 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);
}
I don't know what's my error, I've searched over internet about similar errors but no one solution I've read has worked (forgive my bad english).
Thanks a lot!
解决方案 Yes, you have a composite primary key for the Devolucion
class, and you are trying to refer to it by just one simple column/field devolucion_master.devolucion_consecutivo
.
I don't know of a way to easily tell Hibernate that devolucion_consecutivo
column is actually of type dunosusa.pos.model.DevolucionId
.
That's why I have never used composite primary keys. I always have one primary key of type Long
(bigint
) and enforce uniqueness for a composition of foreign keys where it's needed. It's easier to work with, no issues like the one you're having.
APPENDIX:
I just did some digging and, actually, there is a way to do it :-)
But as I said it's not as easy as using single column PK/FK keys.
Instead of this:
<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>
...
You should list all PK columns in the FK like this:
<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>
...
A little advice - use the package
attribute of the hibernate-mapping
element so you don't have to type it in everywhere - it makes it more readable.
Good luck.
这篇关于org.hibernate.MappingException:外键(FK12A711396456CA10 :)必须与引用的主键具有相同的列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文