I have a problem creating a 1:N relationship between two entitys without using "sets" or so in one entity. I would guess that I have a wrong understanding of how this would work.
The example looks like this: A message system knows users, topics and messages. Users and topics don't depend on anything while messages want to know 1) a user, 2) a topic as well as 3) a root message (in some cases)
The java code looks like this:
class User {
private String name;
// ... other members
// ... getter & setter etc
}
class Topic{
private String title;
// ... other members
// ... getter & setter etc
}
class Message {
private Long messageID;
private Long rootID; // if the message is a response to another message
private Topic topic;
private User user;
// ... other members
// ... getter & setter etc
}
The point here is that I don't use sets of messages in topic or user. It is totaly sufficient if a message has references to the user and the topic. There are some other examples in the internet which explain how to create relationships (1:N) between two objects where one object in java has sets or lists of the other object. But in my example, that's not the case.
I use hibernate with XML, here is what the mappings look like
<!-- USER -->
<hibernate-mapping>
<class name="User" table="User">
<id name="name" type="string" column="Name" />
</class>
</hibernate-mapping>
<!-- USER -->
<hibernate-mapping>
<class name="Topic" table="Topic">
<id name="title" type="string" column="Title" />
</class>
</hibernate-mapping>
<!-- MESSAGE-->
<hibernate-mapping>
<class name="Message" table="Message">
<id name="messageID" type="long" column="MessageID" />
<!-- what comes next? -->
</class>
</hibernate-mapping>
How could I bring hibernate to create the correct objects in the database? I can add / load and delete users and topics, messages also are created with the correct members besides the references, these are set to NULL. I tried to use one-to-one and one-to-many but neither worked.
No comments:
Post a Comment