我有2个表email_address和group_email
我想获取一个电子邮件地址列表,如果是组,还会列出组的电子邮件。这是我的hibernate dom对象:
@Entity
@Table(name = "email_address")
public class EmailAddressDom {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "email_address_id_seq")
@SequenceGenerator(name = "email_address_id_seq", sequenceName = "email_address_id_seq")
private int id;
@Column(name = "name")
private String name;
@Column(name = "email")
private String email;
@Column(name = "is_group")
private boolean isGroup;
@Column(name = "status")
private boolean status;
// Getters and setters
以下是我如何获取列表的实现
public List<EmailAddressDom> getEmailAddresses() {
Session session = getCurrentSession();
Criteria criteria = session.createCriteria(EmailAddressDom.class);
List<EmailAddressDom> emailAddresses = criteria.list();
return emailAddresses;
}
结果是
[
{
"id": 451,
"name": "HS",
"isGroup": true,
"status": true
},
{
"id": 452,
"name": "test4",
"email": "test4@mail.com",
"isGroup": false,
"status": true
},
{
"id": 450,
"name": "test3",
"email": "test3@mail.com",
"isGroup": false,
"status": true
},
{
"id": 402,
"name": "test2",
"email": "test2@mail.com",
"isGroup": false,
"status": true
},
{
"id": 401,
"name": "test1",
"email": "test1@mail.com",
"isGroup": false,
"status": true
}
]
我想得到像这样的结果
[
{
"id":451,
"name":"HS",
"isGroup":true,
"status":true,
"groupEmails":[
{
"id":450,
"name":"test3",
"email":"test3@mail.com",
"isGroup":false,
"status":true
},
{
"id":452,
"name":"test4",
"email":"test4@mail.com",
"isGroup":false,
"status":true
}
]
},
{
"id":402,
"name":"test2",
"email":"test2@mail.com",
"isGroup":false,
"status":true
}
]
我应该在EmailAddressDom实体中使用哪种映射?
根据评论更新
我已经按照以下方式编写了一个简单的应用程序映射,并且工作正常
@ManyToMany(fetch= FetchType.EAGER, cascade={CascadeType.ALL})
@JoinTable(name="group_email",
joinColumns={@JoinColumn(name="group_id")},
inverseJoinColumns={@JoinColumn(name="email_addr_id")})
private List<EmailAddressDom> groupEmails;
但是我很困惑在我的网络应用程序中设置它,它抛出以下异常:
Association has already been visited: AssociationKey(table=group_email, columns={email_addr_id})
可能是什么原因?
答案 0 :(得分:0)
要实现自我加入,请在EmailAddressDom
类中添加对自己的引用,如:
@OneToMany(fetch = FetchType.LAZY)
@JoinTable(name = "group_email",
joinColumns = @JoinColumn(name = "email_addr_id", referencedColumnName = "id"),
inverseJoinColumns=@JoinColumn(name="group_id", referencedColumnName="id" ))
private List<EmailAddressDom> emailAddressDoms;
在group_email
表格中,您不需要id
列email_addr_id
和group_id
组合是唯一的,或者您是否在群组中多次允许同一封电子邮件?< / p>
OP发表评论后更新: 要按电子邮件组进行分组,请更改映射,如下所示。
@ManyToOne(fetch = FetchType.LAZY)
@JoinTable(name = "group_email",
joinColumns = @JoinColumn(name="group_id", referencedColumnName="id" ),
inverseJoinColumns=@JoinColumn(name = "email_addr_id", referencedColumnName = "id"))
private List<EmailAddressDom> emailAddressDoms;
答案 1 :(得分:0)
要么加载
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "group_email",
joinColumns = @JoinColumn(name = "email_addr_id", referencedColumnName = "id"),
inverseJoinColumns=@JoinColumn(name="group_id", referencedColumnName="id" ))
private List<EmailAddressDom> emailAddressDoms;
OR
//明确加入条件
public List<EmailAddressDom> getEmailAddresses() {
Session session = getCurrentSession();
Criteria criteria = session.createCriteria(EmailAddressDom.class);
criteria.setFetchMode("emailAddressDoms", fetchMode.JOIN);
List<EmailAddressDom> emailAddresses = criteria.list();
return emailAddresses;
}