If do not have time please have a look at the example
I have two types of users, temporary users and permanent users.
Temporary users use the system as guest just provide their name and use it but system needs to track them.
Permanent users are those that are registered and permanent.
Once user create a permanent record for himself, I need to copy all the information that has been tracked while user was a guest to his permanent record.
Classes are as following,
@Entity
public class PermUser{
@Id
@GeneratedValue
private long id;
@OneToMany
private List Favorites favorites;
....
}
@Entity
public class Favorites {
@Id
@GeneratedValue
private long id;
@OneToMany (cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List <FavoriteItems> items;
...
}
@Entity
public class FavoriteItems {
@Id
@GeneratedValue
private long id;
private int quantity;
@ManyToOne
private Ball ball;
..
}
@Entity
public class TempUser extends PermUser{
private String date;
....
}
Problems is :
If I clone the tempUser object, I am copying the id parameters as well so when saving the perm user object it shows a message like "Duplicate entry '10' for key ...", I can not remove the tempUser first then save the permUser as if saving permUser failed I will miss the data. If I try to copy each ball of favoriteitems separately without id of item it would not be an efficient way.
Example (Question in one sentence: As shown blew a user may have more than one TempUser record and just one PermUser record, therefore I need to add information of all the TempUser records to that single PermUser record.)
Type of record | name | favorites | date
| | |
1)TempUser | Jack | 2 items | 1/1/2013
2)TempUser | Jack | 3 items | 1/4/2013
---------------------------------------------------------------------------
PermUser | Jack | 5 items ( 2 + 3 items from his temp records)
*Please note, I need to find a solution, and do not care if try a new solution rather than cloning the object.
The reason that I have two different classes is that tempUser
has few additional attributes, I may also need to add favorites of few tempUsers
to favorites list of one permUser
. and also as mentioned above a user may have many different not related temp records