I want to link two entities with a many-to-one link, but so that the class of the many entity does not store a reference to an object of the class one.

In general, I want something like this:

@Entity(name = "A") @Table(name = "As") public class A implements Serializable { @Id @Column(name = "A_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; } @Entity(name = "B") @Table(name = "Bs") public class B implements Serializable { @Id @Column(name = "B_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @ManyToOne(targetEntity = A.class) @JoinColumn(name = "A_id", referencedColumnName = "A_id") private long owner; } 

Here, instead of object A the value of its id is stored in the field owner .

However, when I try to use such code ( session.save(objectB); ), I get exceptions from the Error accessing field series.

All setters are setters.

So the question is: How can we connect two classes of only one field so that there are no such problems?

PS I need exactly

 private long owner; 

but not

 private A owner; 

    2 answers 2

    It is just impossible to link across the field, there must be another table. This is achieved using the @JoinTable annotation in class B:

     @OneToMany JoinTable(name="AB", @JoinColumn(name="a_id"), inverseJoinColumns=@JoinColumn(name="b_id") private List<A> owner; 

      The easiest way is not to designate a connection in principle. Specify it only at the database level, and in the program, designate it as a regular column with the long format.