tags:

views:

307

answers:

1

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

+1  A: 

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.

David M
i only read relation basic mapping section, i will read inheritance section. thanks
QuanNH
ok, i edited it
QuanNH
for simple, i added id field in scheduled_message table (ugly), and it work well :|, thank you
QuanNH