开发者

hibernate: foreign key is primary key

i have 2 tables relation one-to-one:

**message**(id, name, content)

**scheduled_message**(message_id, start_time, stop_time)

i use message_id as primary key of scheduled_message table.

my domain开发者_Go百科 class:

public class Message {

    private Integer id;
    private String name;
    private String content;
    ...
}

public class ScheduledMessage {
 private Message message;
 private Date startTime;
 private Date stopTime;
}

i try write hibernate xml config to map 2 classes, but still error in message_id :(

<class name="Message" table="message">
  <id name="id" column="id">
   <generator class="native" />
  </id>
  <property name="name" column="name" />
  <property name="content" column="content" />
 </class>

<class name="ScheduledMessage" table="scheduled_message">
  <id name="message" column="message_id">
   <generator class="foreign">
    <param name="property">message</param>
   </generator>
  </id>
  <property name="startTime" column="start_time" />
  <property name="stopTime" column="stop_time" />
  <one-to-one name="message" constrained="true"/>
 </class>

error: Could not determine type for: cbs.domain.Message, at table: scheduled_message, for columns: [org.hibernate.mapping.Column(message_id)]

help me plz

thanks

Quan


What you have here can be looked at in a different way. Rather than having ScheduledMessage expose a Message property, you could look at it that ScheduledMessage is a specialised type of Message - in other words a subclass. Hibernate allows you to map inheritance relationships like this in a number of ways - what you have here is called "table per subclass". If you read the section of the Hibernate documentation on this sort of mapping, it should make everything clear and give you enough example code to get you up and running.

Alternatively, to stick with your current approach, you could map the id of ScheduledMessage as a composite id but with just a single component.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜