2017-04-19 104 views
0

我使用JPA 2.0在我的项目,我用我的POJO类属性的一个@ElementCollection。数据表中重新插入使用@ElementCollection上设置<String>

以下是我的Java实体类:

@Entity 
@Embeddable 
@Table(name = "test") 
public class Test { 

@Id 
@GeneratedValue(generator = "seq_test") 
@SequenceGenerator(name = "seq_test", sequenceName = "seq_test") 
@Column(name = "id") 
private Long id; 

@ElementCollection(fetch = FetchType.LAZY, targetClass = String.class) 
@CollectionTable(name = "denied_set") 
@OrderColumn 
private Set<String> deniedSet; 

@ElementCollection(fetch = FetchType.LAZY, targetClass = String.class) 
@CollectionTable(name = "masked_set") 
@OrderColumn 
private Set<String> maskedSet; 

@ElementCollection(fetch = FetchType.LAZY, targetClass = String.class) 
@CollectionTable(name = "user_groups") 
@OrderColumn 
private Set<String> userGroups; 

public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 

public Set<String> getDeniedSet() { 
    return deniedSet; 
} 

public void setDeniedSet(Set<String> deniedSet) { 
    this.deniedSet = deniedSet; 
} 

public Set<String> getMaskedSet() { 
    return maskedSet; 
} 

public void setMaskedSet(Set<String> maskedSet) { 
    this.maskedSet = maskedSet; 
} 

public Set<String> getUserGroups() { 
    return userGroups; 
} 

public void setUserGroups(Set<String> userGroups) { 
    this.userGroups = userGroups; 
} 

} 

我使用Spring JPA的数据存储库与数据库交互。数据正确插入到数据库表中。当我从表中删除一些数据,我可以在删除查询的子表也和相同的数据重新插入到表之后执行的日志中看到。

谁能帮我明白,如果我缺少的代码什么?

在此先感谢。

回答

0

只是一些问题,可以帮助你:

  1. 你需要@Embedded公共类测试?如果您没有在 另一个拥有实体中使用它,请将其删除。

  2. 指定哪个列应该用作 @ElementCollection的ID。使用这样的事情: @CollectionTable(NAME = “denied_set”, JoinColumns = @ JoinColumn(name = “COLUMN_NAME”)),或使用嵌入式如果 deniad_set是嵌入。

  3. 提取类型上默认ElementCollection懒惰,你真的需要targetClass吗?

否则,它看起来是正确的,我...

相关问题