-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Mapping Directed Graph between unknown Objects
PostPosted: Tue Jun 19, 2012 8:16 pm 
Newbie

Joined: Tue Jun 19, 2012 7:40 pm
Posts: 1
Hi,

i have a problem to map a directed graph between objects. I have the following Mapping Description added to an object:

Code:
@Entity
@Table(name="USER")
public class User {

   @Id
   @Column(name = "ID", nullable = false)
   private long Id;

   @ElementCollection(fetch = FetchType.EAGER)
   @CollectionTable(name = "FOLLOWERS", joinColumns = { @JoinColumn(name = "FRIEND_ID") })
   @ForeignKey(name = "none")
   @Column(name = "FOLLOWES_ID", nullable = false)
   @SQLInsert(sql = "INSERT IGNORE INTO FOLLOWERS (FOLLOWES_ID, FRIEND_ID) VALUES (?,?)")
   @SQLDelete(sql = "DELETE FOLLOWERS WHERE FOLLOWES_ID = 1 and 1<>2")
   @SQLUpdate(sql = "UPDATE FOLLOWERS WHERE FOLLOWES_ID = 1 and 1<>2")
   private Set<Long> followers = new HashSet<Long>();

   @ElementCollection(fetch = FetchType.EAGER)
   @CollectionTable(name = "FOLLOWERS", joinColumns = { @JoinColumn(name = "FOLLOWES_ID") })
   @ForeignKey(name = "none")
   @Column(name = "FRIEND_ID", nullable = false)
   @SQLInsert(sql = "INSERT IGNORE INTO FOLLOWERS (FRIEND_ID, FOLLOWES_ID) VALUES (?,?)")
   @SQLDelete(sql = "DELETE FOLLOWERS WHERE FRIEND_ID = 1 and 1<>2")
   @SQLUpdate(sql = "UPDATE FOLLOWERS WHERE FRIEND_ID = 1 and 1<>2")
   private Set<Long> friends = new HashSet<Long>();

   // Getter and Setters
}



Code:
// user1 with a follower relation to user2
User user1 = new User();
user1.setId(1l);
user1.getFollowers().add(2l);
Transaction transaction = session.beginTransaction();
session.saveOrUpdate(user1);
transaction.commit();

// Create and save previously unsaved user but not set the Friend relation to user1
User user2 = new User();
user2.setId(2l);
transaction = session.beginTransaction();
session.saveOrUpdate(user2);
transaction.commit();

// Load user2 from db and user2 friends is empty (same for followers of user1)
User user2loaded = (User) session.byId(User.class).load(2l);
System.out.println(user2loaded.getFriends().toString() + user2loaded.getFollowers().toString());

The @SQLInsert is getting used, but neither the @SQLDelete nor the @SQLUpdate. I don't want to delete anything in the FOLLOWERS table.
I know this is a little bit awkward mapping. But not all objects are always available and followers/friends can be empty (or not complete) but should never trigger any deletion. Is this even possible with Hibernate?

Thank for your help!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.