-->
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.  [ 8 posts ] 
Author Message
 Post subject: saving id to database instead of the whole object
PostPosted: Fri Aug 21, 2009 8:31 am 
Newbie

Joined: Fri Aug 21, 2009 8:18 am
Posts: 5
Hi there,

I have an many-to-many relationship, to which I want to add an additional table. I'm using the instructions here viewtopic.php?f=1&t=950062&start=0

In my many-to-many relationship, I also want the ID to be the connection of the both connected many-to-many relationship. The problem is that, when I create the @IdClass to define this, Hibernate wants to save the whole object to db and not just the id of the entity.

So my question is, how do I tell to hibernate that I want to save only the ID of the object to database, not the whole object?


Here are my classes:

@Entity
public class User implements Serializable {

@OneToMany (targetEntity=Friend.class,mappedBy="user")
private List<Friend> friends = new LinkedList<Friend>();

//I have tried with and without this component, I don't need this if Hibernate doesn't require it
@OneToMany (targetEntity=Friend.class,mappedBy="friend")
private List<Friend> otherFriends = new LinkedList<Friend>();

@Id @GeneratedValue (strategy = GenerationType.AUTO)
private Integer id;

... the getters and setters etc. other data...

}

@Entity
@IdClass(FriendId.class)
public class Friend implements Serializable {
@NotNull
private boolean pending;

@Id
@ManyToOne
@NotNull
private User user;

@Id
@ManyToOne
@NotNull
private User friend;

...the getters and setters ...

}

I have two solutions for the Friend Primary key table:

This one doesn't let the Hibernate to insert the table, but says:
BLOB/TEXT column ‘friend’ used in key specification without a key length
public class FriendId implements Serializable {
private User user,friend;

... the getters and setters...

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
else if (o instanceof FriendId) {
return friend.equals(((FriendId)o).friend) &&
user.equals(((FriendId)o).user);
}
return false;
}

@Override
public int hashCode() {
return user.hashCode() ^ friend.hashCode();
}
}

This one creates the table but Hibernate doesn't understand that only the user-id is stored, but looks for the whole object from the database
public class FriendId implements Serializable {
private int user,friend;

... the getters and setters...

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
else if (o instanceof FriendId) {
return friend == ((FriendId)o).friend &&
user == (((FriendId)o).user);
}
return false;
}

@Override
public int hashCode() {
return user ^ friend;
}
}

Thanks,

Jesse


Top
 Profile  
 
 Post subject: Re: saving id to database instead of the whole object
PostPosted: Fri Aug 21, 2009 11:54 am 
Beginner
Beginner

Joined: Wed Sep 06, 2006 12:08 pm
Posts: 48
Location: Warsaw - Poland - Europe - Milky Way
My 3 cents:
Hmmm...
Does it solve your problem? --->

Code:
@org.hibernate.annotations.Entity(dynamicInsert=true)


When dynamicInsert is set to true, then *only* columns modified by you (for example id column) will be present in SQL INSERT statement generated by you.

Take care!
Adam Wozniak


Top
 Profile  
 
 Post subject: Re: saving id to database instead of the whole object
PostPosted: Mon Aug 24, 2009 3:02 am 
Newbie

Joined: Fri Aug 21, 2009 8:18 am
Posts: 5
Didn't seem to help. It caused another Exception. I'm actually using javax.persistence.Entity, and it doesn't seem to have the dynamicInsert variable. Changing to org.hibernate.annotations.Entity causes another Exception.


Top
 Profile  
 
 Post subject: Re: saving id to database instead of the whole object
PostPosted: Mon Aug 24, 2009 4:49 am 
Beginner
Beginner

Joined: Wed Sep 06, 2006 12:08 pm
Posts: 48
Location: Warsaw - Poland - Europe - Milky Way
Nope :)

Don't try to replace avax.persistence.Entity with @org.hibernate.annotations.Entity :)
Just add this as another annotation to you Java class. Something like this:

Code:
@Entity
@Table(name="PROFILE_SERVICE")
@org.hibernate.annotations.Entity(dynamicInsert=true, dynamicUpdate=true)


Take care!
Adam Woźniak
Poland


Top
 Profile  
 
 Post subject: Re: saving id to database instead of the whole object
PostPosted: Tue Aug 25, 2009 2:08 am 
Newbie

Joined: Fri Aug 21, 2009 8:18 am
Posts: 5
Hmh, now it just printed the same error message as previously:
BLOB/TEXT column ‘friend’ used in key specification without a key length

So it seems that this doesn't fix the problem.

I want to save these to fields as int (the ID:s) to db and not the whole object:

@Id
@ManyToOne
@NotNull
private User user;

@Id
@ManyToOne
@NotNull
private User friend;

And I don't know how to tell that to the machine.

br,

Jesse


Top
 Profile  
 
 Post subject: Re: saving id to database instead of the whole object
PostPosted: Tue Aug 25, 2009 8:14 am 
Newbie

Joined: Sat May 26, 2007 8:18 am
Posts: 9
精华啊 不过我已经看过了 哈哈 还是顶下 *^__^*








------------------------------------------------------------
为什么是中域互联选择了我,而不是啊红选择了我,为什么???


Top
 Profile  
 
 Post subject: Re: saving id to database instead of the whole object
PostPosted: Wed Sep 02, 2009 4:18 am 
Newbie

Joined: Fri Aug 21, 2009 8:18 am
Posts: 5
Hi,

Still looking the answer. I actually need another table like that also, so it would be nice to use the proper way.

br,

Jesse


Top
 Profile  
 
 Post subject: Re: saving id to database instead of the whole object
PostPosted: Wed Sep 09, 2009 12:51 pm 
Newbie

Joined: Fri Aug 21, 2009 8:18 am
Posts: 5
Hi,

Adding ManyToOne annotation to the class defining the PK (FriendId.class in this example) seems to do the trick:

public class FriendId implements Serializable {
@ManyToOne
private User user,friend;

br,

Jesse


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

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.