0

I need map at JPA something like this:

https://i.stack.imgur.com/9ve1W.png

How I can map two relationships between two "tables", one of them is primary key and one to one (newClient in advance) and in the other side, a one to many that aint PK?

I tried something like this but it fails.

public class Recommendation implements Serializable {
    @Id @OneToOne
    @Column(name="...")
    private Client newClient;
    @ManyToOne
    @Column(name="...")
    private Client oldFella;
    @Column(name="...")
    private Boolean wasUsedToGenerateBond;

...

}

Thanks!

nanofarad
  • 40,330
  • 4
  • 86
  • 117
tehAnswer
  • 960
  • 1
  • 13
  • 28

2 Answers2

1

it is possible to have multiple ManyToMany relationships between two entities. Each relationship would have its own join table. The default name of the join table may be the same, so you would need to specify the @JoinTable's name.

possible answer here

JPA Problems mapping relationships

more information

Hibernate 4.2, bidirectional @OneToOne and @Id

Community
  • 1
  • 1
0

Like this:

@Id @OneToOne
@JoinColumn(name="id_client1")
private Client newClient;
@ManyToOne
@JoinColumn(name="id_client2")
private Client oldFella;
Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36
  • It doesn't work. I got in the Class Client something like: `@OneToMany(fetch = FetchType.EAGER,mappedBy = "newClient") private Set recommendations = new HashSet(); @ManyToOne private Client theGuyWhoRecommendsMeThis;` – tehAnswer Nov 27 '13 at 15:04
  • Unable to configure EntityManagerFactory. `mappedBy reference an unknown target entity property: uo.ri.model.Recommendation.newClient in uo.ri.model.Cliente.recommendations` – tehAnswer Nov 27 '13 at 15:37
  • Just checking: do you have getters and setters for your fields? You should. Also, you can try applying your annotation to the public members (getters in your case). – Andrei Nicusan Nov 27 '13 at 15:43
  • I got getters and setters (except Id attrb, because I generate hash code and equals on them). – tehAnswer Nov 27 '13 at 15:55
  • Try to move your annotations to getters, then. – Andrei Nicusan Nov 27 '13 at 15:58