2

I don't know how i can do that, or what kind of relation i will choose.

I have 3 tables and 1 association which is :

Client -----(0,n)---- (Command) -----(0,n)----- Service

Target is also attached with the (command) association, and Target can be Null in Command (0,1). The relation between Command and Client is bidirectional.

For me, the class diagram will be like this :

UML

But i don't know how to proceed, if i create an entity in Command with a @ManyToOne, or other method. My real problem is about the association with three tables. Could you help me ?

toshiro92
  • 1,287
  • 5
  • 28
  • 42

2 Answers2

3

You're going to need to use @ManyToOne as you suggest. EclipseLink has a great set of documentation for JPA (even if you're using a different implementation)

In Command you'll have this:

@ManyToOne
@JoinColumn(name="CLIENT_ID", nullable=true)
private Client client;

And in Client you'll have this:

@OneToMany(mappedBy="client")
private Set<Command> commands;
Erich
  • 2,743
  • 1
  • 24
  • 28
1

This link gives a pretty good example. However, the api for both the OneToMany and ManyToOne annotation both give pretty decent examples of how to use them. This is usually a good place to start. You just need to determine which side of the relationship will own it.

Community
  • 1
  • 1
John Kane
  • 4,383
  • 1
  • 24
  • 42
  • I thougth i should have used another kind of structure, like an association ManyToMany with a table. But this is better, thanks for the link ! – toshiro92 Apr 26 '13 at 13:59
  • you would only use ManyToMany if clients would use one command and many commands would be associated with one client. You have it mapped as a OneToMany relationship from client to command, which seems to make sense. – John Kane Apr 26 '13 at 14:07