aspose file tools*
The moose likes Object Relational Mapping and the fly likes Automatically Persisting non existing entity in One To One mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Automatically Persisting non existing entity in One To One mapping" Watch "Automatically Persisting non existing entity in One To One mapping" New topic
Author

Automatically Persisting non existing entity in One To One mapping

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

I have a OneToOne relationship between two classes. One of the entity is User which has a lot of records in the database. I added the second entity, UserTheme, new to the database. This is the structure of the two classes



Now the problem is that the new table doesn't have any records. But I wan an entry in the UserTheme corresponding to the User table. I don't know how can I do this. I just want some kind of configuration that when a user access the UserTheme property. I tried this kind of thing but failed



This code throws a TransientObjectException on calling getUserTheme method when userTheme is null. I don't know what changes to make to make this work. The only thing that I can't do is add the columns related to user theme into the user table and have an embedded entity. Other than that please provide me a solution to this.

Thanks in advance...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10263
    
168

One of the entity is User which has a lot of records in the database. I added the second entity, UserTheme, new to the database
...
Now the problem is that the new table doesn't have any records. But I wan an entry in the UserTheme corresponding to the User table.


You mean you want to add the UserTheme for each of the existing User records?


[My Blog] [JavaRanch Journal]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Jaikiran Pai wrote:You mean you want to add the UserTheme for each of the existing User records?


Yes. And I don't know how to do that. I was thinking about two solutions. I showed one of them. The other was to persist an instance of UserTheme in the getUserTheme method but I think that would be a very bad design. I don't know of any other solution. May be I have to run a SQL query like select into or something but I'm not sure. It would be great if this problem is solved without using an SQL query like select into etc...
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10263
    
168

I believe this is going to an one time activity since you already have users who do not have userthemes. Henceforth any new user will have a usertheme associated by default (that's what i gather from what you mentioned). So i would suggest going for the SQL query approach for a couple of reasons:

1) It would be simpler
2) If there are a lot of already existing users, then creating new userthemes for those users through SQL would be better in terms of performance, instead of having a custom (one time) code that does this.

However, if you still want to do it the Java/JPA way, then here's what you could do:

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Something strange happened. When I used this code



then it persisted the UserTheme object in the database automatically but then couldn't set the foreign key column of the User table. Also Jaikiran if you insist the sql solution, then can you please help me with the sql query. I cannot think about any query to use. I know I am asking too much but if you can, please help me. Actually I was looking for JPA solution using something with the OneToOne annotation. Since there is no such way, so I will have to resort to the SQL query I think...
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
Did you try the solution posted by Jaikiran.
I suppose it should have worked.
Anyways, if you want to resort to the SQL solution, it should be something like.
Insert into UserTheme(userId) select userId from User;
here i am assuming that userId is the primary key in both the tables, and that there are no other non nullable columns in the UserTheme table.
Alternately, if you want to fill in the columns in the UserTheme table from user table, you can always do something like
Insert into UserTheme(userId, col1, col2....coln) select userId, cola, colb...coln from User;

Hope it helps.

Rahul Babbar
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Thanks Rahul for the help. But the User table has a foreign key to the UserTheme table. I think I'll have to modify the User and UserTheme classes to use PrimaryKeyJointColumn to make the sql statement work. Or I can use the JPA solution given by Jaikiran. So I think I have two possible solutions which puts me in a comfortable state . Thanks Rahul and Jaikiran for the help
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
You dont ned to modify the UserTheme entity to make it work.
If according to the FK,
the id column in Usertheme refers to userId from User,
You can do.
Insert into UserTheme(id) select userId from User.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Automatically Persisting non existing entity in One To One mapping