File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Problem with OneToOne relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem with OneToOne relationship" Watch "Problem with OneToOne relationship" New topic
Author

Problem with OneToOne relationship

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

First of all I am really confused whether to put this in the EJB forum or this forum. But I feel that this is the correct place for it.

I have an existing class and a table for Users. So there is a table in the database named User and a corresponding entity in EJB3 i.e. User.java Now I want to add a new field to the class i.e. for user theme. There are many things the user can choose like home page color, text size etc. So I have created an entity for this i.e. UserTheme. I have also created a new table for the UserTheme class. Now I want a OneToOne mapping between the classes. I only want a references to the UserTheme class in the User class. I can't alter the User table. So I think creating a OneToOne relationship with mappedBy would be my choice. I think for that I'll also have to add User object in the UserTheme class but I'm not sure. Can anyone please help me on this.

Thanks in advance


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Tong Wang
Ranch Hand

Joined: Jun 20, 2005
Posts: 44
Have you thought about an embeddable class? Like this:

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Thanks Tong for the advice. Well the reason I used OneToOne is that due to performance issues I might have to load the UserTheme object lazily. I don't think Embedded allows lazy loading. If it allows can you tell me hot to do that...
Tong Wang
Ranch Hand

Joined: Jun 20, 2005
Posts: 44
Ok, I see. So you'll need a unidirectional one-to-one mapping. User will have a reference to UserTheme, but UserTheme will NOT reference User. Since you mentioned that you can NOT alter user table, you will have to use @PrimaryKeyJoinColumn, meaning that User table and UserTheme table will have the same primary key.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Thanks again Tong for your valuable opinion. Actually I was facing a very strict deadline so I did it before your reply so my solution is different from yours. This is what I did



and in the UserTheme class I added a reference to the User object (I know I told that its not needed but it was not banned too )



And this did the trick for me. Now I just want that when a new row is created in the User table, an entry should be made in the UserTheme table. But this is not happening. I added the cascade type Persist for that. Also I want that when getUserTheme is called and there is no entry in the UserTheme table for the current user, a row must be inserted in the UserTheme table for the user. Do I need to do this manually using the entity manager or is there a way that this can happen automatically??

[edit: added another question ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

I am in a big big problem with the entity structure that I have used. I can't call entityManager.merge on an instance of User class now. Calling that results in a ClassCastException. The jboss console shows this error message



I also removed the CascadeType.PERSIST from the user class to see if that solves the problem but to no effect.

Now I'm clueless about the error. I am really in trouble now. What should I do, can anyone give me some idea...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Ok I used the strategy given by Tong and now I don't have any such problems. Now I can update the User class. But now I just want to know how can I ensure that when a new User entry is inserted, a corresponding entry should be inserted automatically. If there is no automatic way, then I'll insert some code to do that when a new user registers. But the main problem lies with existing users. If there is already a row in the User table, and I call the getter method of UserTheme, I want to insert a row in UserTheme table. I don't know if this can happen automatically. I inserted code to persist a new UserTheme object into the database in the getter method in User class but now the whole code is down. The code is not working at all. What's the solution to this problem
 
 
subject: Problem with OneToOne relationship