I have entity Customer
and entity Ticket
.
Customer
can have two different tickets. List
of Tickets and OneToMany
relationship is not suitable there, because I should have two different fields in Customer
entity for tickets: ownTicket
and refererTicket
. Please help me implement this relationship. How it can be done?
You can have two one to one relationships, like so:
class Costumer {
@OneToOne
Ticket ownTicket;
@OneToOne
Ticket refererTicket;
}
Just give different names. And then, in the Ticket class, if you do want to map both ways, I believe you have to use the mappedBy property:
class Ticket {
@OneToOne(mappedBy="ownTicket")
Costumer owner;
@OneToOne(mappedBy="refererTicket")
Costumer referer;
}
If Ticket can have only one Costumer, then this is what I would do:
Create a relationship class, called TicketCostumer
or something:
class TicketCostumer {
@OneToOne
Ticket ticket;
@ManyToOne
Costumer costumer;
Type type;
}
Where Type is an enum you create that has OWNER and REFERER;
Now each ticket can have only one of this:
class Ticket {
@OneToOne
CostumerTicket ct;
}
And finally, on Costumer, you can decide how to handle things; you could have a list and guarantee by hand that there is not more than one of each kind (or try to use the @UniqueBy or something like that), or you can have two separated fields to control it.
You should map it as a @OneToMany
but hide this implementation detail. This could look as below: ticket has an extra column indicating its type.
@Entity
public class Customer {
@OneToMany
@JoinColumn(name = "customer_id")
@MapKeyColumn(name = "ticket_type")
@MapKeyEnumerated(EnumType.String)
private Map<TicketType, Ticket> tickets;
// we will not expose the tickets collection
// the fact it is a @OneToMany is then transparent to all client code
public Ticket getOwnTicket(){
return tickets.get(TicketType.O);
}
public Ticket getReferrerTicket(){
return tickets.get(TicketType.R);
}
public void setOwnTicket(Ticket ticket){
//ticket.setType(TicketType.O); //may not be required
tickets.put(TicketType.O, ticket);
}
public void setReferrerTicket(Ticket ticket){
//ticket.setType(TicketType.R); //may not be required
tickets.put(TicketType.R, ticket);
}
}
What's the problem?
You can easily create Customer as:
public class Customer{
....
@OneToOne
Ticket ownTicket;
@OneToOne
Ticket refererTicket;
....
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.