我有多对一的单向关系,我试图坚持一个子实体,即SubscriptionEntity也期望它也应该持久保存,因为我使用cascade = CascadeType.PERSIST
关于关系的背景:SubscriptionEntity让您知道哪个用户与哪个商家相关联,并且几乎没有更多的实体喜欢告诉我们州的状态和产品订阅以及与订阅相关的产品是什么。订阅状态是一对多的双向,因为订阅可以在整个成员资格中转移到许多状态 - 保持,活动,停用等。同样,与产品的订阅关系是一对多的双向,因为用户可以订阅一个产品或不止一个。
我们无法在用户和订阅之间进行双向关联,因为用户可以与许多商家建立关联。
public class SubscriptionEntity implements Domain, Serializable
{
@Column(name = "subscription2user")
@ManyToOne(optional = false, fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
@JoinColumn(name = "subscription2user")
private UserEntity user;
@Column(name = "subscription2biz")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "subscription2biz")
private BusinessEntity business;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "subscription", cascade = CascadeType.PERSIST)
private List<ProductSubscriptionEntity> subscribedProducts;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "subscription", cascade = CascadeType.PERSIST)
private List<SubscriptionStateEntity> state;
}
public class UserEntity implements Domain, Serializable
{
private static final long serialVersionUID = 755369097357065341L;
@Id
@Column(name = "user_id")
private String id;
@Column(name = "user_first_name")
private String firstName;
@Column(name = "user_last_name")
private String lastName;
@Column(name = "user_mobile")
private BigInteger mobile;
@Column(name = "user_password")
private String password;
@Column(name = "user_create_date")
private Date createDate;
@OneToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "user2address")
@Column(name = "user2address")
private UserAddressEntity address;
}
public class SubscriptionStateEntity implements Domain, Serializable
{
@Column(name = "subscription_state2subscription")
@ManyToOne(optional = false)
@JoinColumn(name="subscription_state2subscription")
protected SubscriptionEntity subscription;
}
public class ProductSubscriptionEntity implements Domain, Serializable
{
@Column(name = "user_product_subscription2subscription")
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "user_product_subscription2subscription")
private SubscriptionEntity subscription;
}
场景:业务已经存在,但用户是新的,用户和订阅之间的关联也是新的。当我试图添加新订阅低于错误。如果用户已经存在于数据库中,那么它可以顺利运行。
void addSubscription(final UserEntity user, final String businessID)
{
final UserEntity userTemp = this.userDao.readUserByIdOrMobile(user.getId(), user.getMobile());
if (null == userTemp)
{
user.setId(java.util.UUID.randomUUID().toString());
user.setCreateDate(DateUtil.getDateTimestamp());
}
final BusinessEntity business = getBusinessByID(businessID);
SubscriptionEntity subscription = new SubscriptionEntity();
subscription.setUser(user);
subscription.setBusiness(business);
super.getEm().persist(subscription);
}
引起: org.apache.openjpa.persistence.EntityExistsException:无法添加或 更新子行:外键约束失败......参考
user
(user_id
)ON更新没有采取行动的行动
任何帮助非常感谢。提前谢谢。
答案 0 :(得分:0)
看起来您没有在UserEntity实体上提供表和列映射注释。
由于您没有设置UserEntity的ID,您是否定义了id生成策略?如果您没有设置id,则订阅和用户之间没有链接。
您可以使用idE字段上的UserEntity中给出的注释将id生成策略添加为自动增量。这将在创建时生成id。
GeneratedValue(策略= GenerationType.IDENTITY) private String id;
答案 1 :(得分:0)
您没有为用户实体指定任何ID,因为ID是String,它将创建ID为“”的UserEntity。尝试为用户实体ID添加一些id生成策略,否则新创建的用户始终是导致此EntityExistsException
的用户。
P.S。双向关联只意味着你可以从双方获得实体。它与您的业务逻辑没有任何关系。