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...
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?
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...
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:
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...
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.