如何将不同记录的数据添加到单个记录? [英] How to add data of different records to a single record?

查看:108
本文介绍了如何将不同记录的数据添加到单个记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有时间,请查看示例



我有两种类型的用户,即临时用户和永久用户。



临时用户使用系统作为guest提供他们的名字并使用它,但系统需要跟踪他们。

永久性用户是那些已注册且永久的用户。

一旦用户为自己创建了永久记录,我需要将用户作为访客时跟踪的所有信息复制到其永久记录中。



类别如下,

  @Entity 
public class PermUser {
@Id
@GeneratedValue
private long id;

@OneToMany
私人收藏夹收藏夹;
....

}

@Entity
公共类收藏夹{
@Id
@GeneratedValue
私人长ID;

@OneToMany(cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List< FavoriteItems>项目;

...
}

@实体
公共类FavoriteItems {
@Id
@GeneratedValue
私人长ID;

private int数量;

@ManyToOne
私人球球;
..
}


@实体
公共类TempUser扩展PermUser {
private String date;
....
}

问题是:如果我克隆tempUser对象,我也复制了id参数,所以当保存perm用户对象时,它显示一条消息,如重复条目10对于密钥...,我无法先删除tempUser,然后保存permUser,就好像保存permUser失败一样,我将错过数据。如果我尝试分别复制每个最喜欢项目的球而没有项目标识,它将不是一种有效的方式。



示例(一句话中的问题:如图所示,用户可能有多个TempUser记录和一个PermUser记录,因此我需要将所有TempUser记录的信息添加到单个PermUser记录中。)

 记录类型|名称|收藏夹|日期
| | |
1)TempUser |杰克| 2项| 2013年1月1日
2)TempUser |杰克| 3项| 1/4/2013
----------------------------------------- ----------------------------------
PermUser |杰克| 5项(2 + 3项目从他的临时记录)

*请注意,我需要找到一个解决方案,并不在乎如果尝试新的解决方案,而不是克隆该对象。



我有两个不同的类是 tempUser 几乎没有附加属性,我可能还需要添加一些 tempUsers 的收藏夹到一个 permUser 。如上所述,用户可能有许多不同的不相关的临时记录

解决方案

原谅我,如果我缺少一些东西,但我不认为 TempUser PermUser 应该是不同的类。 TempUser 扩展 PermUser ,这是is-a关系。显然,临时用户不是一种永久用户。你的问题并没有给出足够的信息来证明它们与众不同 - 也许它们是同一个类,并且差异可以表示为一些新的属性?例如:

  @Entity 
public class User {
@OneToMany(cascade = CascadeType.ALL)
私人收藏夹收藏夹;
private boolean isTemporary;
....
}

从临时到永久的过渡可以由某个控制器处理,确保 isTemporary = false ,并且永久用户的其他属性被适当设置。这会完全克服克隆问题,并且在您的数据库上会更容易。


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

解决方案

Forgive me if I'm missing something, but I don't think that TempUser and PermUser should be different classes. TempUser extends PermUser, which is an "is-a" relationship. Clearly, temporary users are not a type of permanent user. Your question doesn't give enough information to justify making them different -- perhaps they're the same class, and the difference can be expressed as a few new attributes? Eg:

@Entity
public class User{
    @OneToMany(cascade = CascadeType.ALL)
    private List Favorites favorites;
    private boolean isTemporary;
    ....
}

The "transition" from temporary to permanent can be handled by some controller, making sure that isTemporary = false and that the other properties of a permanent user are appropriately set. This would completely side-step the cloning issue and would be much easier on your database.

这篇关于如何将不同记录的数据添加到单个记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆