如何在hibernate中使用两列作为主键 [英] how to use two columns as primary key in hibernate
问题描述
我已阅读复合键,但它认为不适合我。毕竟,我只有一张桌子,在我的代码中不会有一对多或类似的基数。
我尝试了类似这样的东西,不工作。
<?xml version =1.0?>
<!DOCTYPE hibernate-mapping PUBLIC
- // Hibernate / Hibernate映射DTD // EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0。 DTD>
< hibernate-mapping>
< class name =blog.Messagetable =MESSAGES>
< id name =titlecolumn =MESSAGE_TITLE>< / id>
< id name =authorcolumn =MESSAGE_AUTHOR>< / id>
< property name =bodycolumn =MESSAGE_BODY/>
// ....以及另外两个字段
< / class>
< / hibernate-mapping>
执行此操作的方法是?复合钥匙是必须的吗?
在此先感谢
理想情况下,如果可能的话,应该避免使用休眠自然键。如果你创建一个id列,Hibernate可以更好地工作,hibernate可以用来管理行的身份。
I have a table with five columns. The two columns, together, make up the primary key of my table. I'm trying to instantiate an Entity class that corresponds to that table using hibernate. The problem is how do I tell hiberante that the primary key is not just a single field of the class but two. I don't want hiberante to create the database schema for me as I have already crated it with all the necessary primary key settings.
I have read about the composite-key but it think that is not suitable for me. After all, I only have one table and there won't be one-to-many or similar cardinality in my code.
I tried something like this which apparently ain't working.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="blog.Message" table="MESSAGES">
<id name="title" column="MESSAGE_TITLE"></id>
<id name="author" column="MESSAGE_AUTHOR"></id>
<property name="body" column="MESSAGE_BODY"/>
// ....AND TWO MORE FIELDS
</class>
</hibernate-mapping>
What is the way to do it? Is composite-key a must? Thanks in advance
Ideally you should avoid natural keys with hibernate, if at all possible. Hibernate will work much better if you create a single id column, that hibernate can use to manage the identity of the row.
这篇关于如何在hibernate中使用两列作为主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!