-->
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.  [ 14 posts ] 
Author Message
 Post subject: Cascade not working
PostPosted: Wed Jun 04, 2008 5:05 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
hi,

this is my situation:

Code:
@ManyToMany(fetch=FetchType.EAGER, cascade={CascadeType.ALL})
@org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.ALL})
@JoinTable(name="User_Details",joinColumns={@JoinColumn(name="username",referencedColumnName="username")}, inverseJoinColumns={@JoinColumn(name="authority",referencedColumnName="authority")})
    private Set<Authority> authlist;


Basicly i have 2 Tables Users and Authorities both of them have a Integer Id as Primary Key. I want to store the username from Users and the authority from Authorities into the JoinTable.
But when i make service.read(1); for the first user for example the
authlist's size is 0 meaning nothing is in there even if the Table user_details contains data. Somehow it doesnt fetch the values?
Somebody know how to fix this??

thx kuku[/u]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 5:21 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
i think the problem is that i dont use the pk id as fk in user_details so hibernate don'T know how to fetch or so .. but i don't understand why this doesnt work .. or how to fix it. the hole point of my manytomany was that i have a jointable where i have the username and the authoritys at one location.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 6:21 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
I think the problem is that i dont use the pk id as fk in user_details

you're right, you have to use the PKs.
Nothing stops you to also store the usernames and more data in the same join table.

BTW this has nothing to do with "Cascade"

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 6:29 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
ok thanks but how do i tell that i want another field in that jointable which is not an FK ? i know how to add more joincolumns but i think thats not the correct way.
edit: yes seems that is a mapping problem and not cascade issue.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 6:36 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
I want to store the username from Users and the authority from Authorities into the JoinTable.

You mean you only want username and authority in the jointable?
this really looks bad design, I would suggest to use PK and FK, why would you want to avoid the two integers?
using the integer PK will perform much better and is safer for your data integrity.

Mybe I'm completely wrong, could you explain the reason for this requirement?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 6:40 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
it's okay to have the id's in the jointable as FK's, but i dont know how to add username and authority as normal fields into this jointable too thats my problem.

The reason why i want this 2 fields in the table is that i can access them easy like select username authority from user_details where username=?.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 6:56 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
So I understand you want the list of authorities for a known username?
why not just

Code:
User user = s.createQuery("from User user where user.username='kukudas'").singleResult();
Set<Authority> authlist = user.getAuthList();


or
Code:
List authlist = s.createQuery("select user.authlist from User user where user.username='kukudas'").list();

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 7:29 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
gonna try it tommorrow. the reason why i want in user_details username and authority is because i'm using acegi security and in the spring configuration i have to set an sql statement how to fetch the username and authority so i can get authentication. if i don't have them in one table i dont know how to fetch them because my knowdgledge of sql is very poor. for now im using select username,authority from user_details where username=?.
if i have a construct where i have only user_id and auth_id in user_Details i don't know how to make an sql querry to read the username and authority for a specific user "?".


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 9:58 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Indeed, this has to do with the mapping of the keys to the join table.

For more help on mapping many to many associations with Hibernate and JPA, you may be interested in this tutorial. There are also discussions on CascadeType and Lazy Loading:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=19mappingmanytomanyrelationships

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 12:19 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi kukudas,
thanks now I understand; If I may offer my advice is to not build your application on some strange model just because you would have to learn a bit of SQL.
the SQL query you have to write is really simple, you should be able to write it following some simple tutorial about JOIN.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 12:22 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
BTW to Cameron:
nice examples on your website! thanks.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 5:42 pm 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
your right i should not base my model on some weak knowledge of sql. so it is basicly best way like this:
1. Table User with Id as PK and some values
2. Table Authority with Id as PK and one Field which containts the Authority.
and the join table which contains User.Id and Authority.Id and nothing more (not even a own ID). It may seem simpel but im still beginner and ive changed my datamodell so often which makes me sick its really a pain in the ass so this time i wanna do it right =).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 5:45 pm 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
i will look into the tutorial next day thanks cameron ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 3:45 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
Ok here i'm again i've read the tutorial it was really great thanks for that. I have a question as u can see in my first posting i did the complete JoinTable on one side (in User class) is this ok? In the tutorial it is made for each side the reference.

my other side looks like this (in Authority):
Code:
    @ManyToMany(mappedBy="authlist",fetch=FetchType.EAGER)
    private List<User> userlist;


for now everything is working fine adding and removing elements of the collection and updating database. The only thing i need now is a appropriate sql or hql statement to fetch the username and authority to a specific user.
i will now look into JOIN. Thanks again for your help guys ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 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.