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 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...
Joined: Jun 20, 2005
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.
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??
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...
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