I 2 Tabellen email_address undHibernate Mapping-Liste innerhalb Liste
ich eine Liste von E-Mail-Adressen group_email erhalten möchten und wenn es Gruppe ist auch die E-Mails der Gruppenliste. Hier ist meine Hibernate dom Objekt:
@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
Und hier ist die Umsetzung, wie erhalte ich die Liste
public List<EmailAddressDom> getEmailAddresses() {
Session session = getCurrentSession();
Criteria criteria = session.createCriteria(EmailAddressDom.class);
List<EmailAddressDom> emailAddresses = criteria.list();
return emailAddresses;
}
Und das Ergebnis ist
[
{
"id": 451,
"name": "HS",
"isGroup": true,
"status": true
},
{
"id": 452,
"name": "test4",
"email": "[email protected]",
"isGroup": false,
"status": true
},
{
"id": 450,
"name": "test3",
"email": "[email protected]",
"isGroup": false,
"status": true
},
{
"id": 402,
"name": "test2",
"email": "[email protected]",
"isGroup": false,
"status": true
},
{
"id": 401,
"name": "test1",
"email": "[email protected]",
"isGroup": false,
"status": true
}
]
Ich möchte wie dieses Ergebnis erhalten
[
{
"id":451,
"name":"HS",
"isGroup":true,
"status":true,
"groupEmails":[
{
"id":450,
"name":"test3",
"email":"[email protected]",
"isGroup":false,
"status":true
},
{
"id":452,
"name":"test4",
"email":"[email protected]",
"isGroup":false,
"status":true
}
]
},
{
"id":402,
"name":"test2",
"email":"[email protected]",
"isGroup":false,
"status":true
}
]
Welche Zuordnung sollte ich in der Entität EmailAddressDom verwenden?
ich eine einfache Anwendungszuordnung wie folgt geschrieben wurden entsprechend Kommentaren aktualisiert und es funktioniert gut
@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;
Aber ich habe verwirrt es in meiner Web-Anwendung einstellen, wirft es folgende Ausnahme :
Association has already been visited: AssociationKey(table=group_email, columns={email_addr_id})
Was kann der Grund sein?
Das DB-Struktur ganz schrecklich ... – meskobalazs