How to use two columns as a primary key in sleep mode

I have a table with five columns. The two columns together make up the primary key of my table. I am trying to create an instance of the Entity class that matches this table using sleep mode. The problem is how to tell hiberante that the primary key is not just one field in the class, but two. I do not want hiberante to create a database schema for me, since I have already completed it with all the necessary primary key settings.

I read about the composite key , but I think this is not suitable for me. In the end, I have only one table, and in my code there will not be one, many, or equal numbers.

I tried something like this, which apparently does not work.

<?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> 

How to do it? Is a composite key required? thanks in advance

0
mapping hibernate composite-primary-key
source share
1 answer

Ideally, you should avoid natural hibernate keys, if at all possible. Hibernate will work much better if you create a single id column that can use hibernation to control the row id.

+1
source share

All Articles