Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate @ManyToOne - only one direction relation

Tags:

hibernate

I got classes:

@Entity
@Table(name="users")
public class User{

private Integer id;
private String name;
private Address address; 
}

and:

    @Entity
    @Table(name="adress")
    public class Adress{

        private Integer id;
        private String street;
        (...)
}

Any way how to map relation @ManyToOne (many users can have the same adres), BUT I don't want to have property List< User > users in my Address class?

like image 337
Cichy Avatar asked Oct 19 '11 12:10

Cichy


People also ask

How does JPA define many-to-one relationships?

Many-To-One relation between entities: Where one entity (column or set of columns) is/are referenced with another entity (column or set of columns) which contain unique values. In relational databases these relations are applicable by using foreign key/primary key between tables.

What is difference between JPA unidirectional OneToOne and ManyToOne?

According to book Pro JPA 2 the main difference between unidirectional @ManyToOne and @OneToOne is that in @OneToOne: Only one instance of the source entity can refer to the same target entity instance. In other words, the target entity instance is not shared among the source entity instances.


1 Answers

Add the annotation @ManyToOne to the address field. Problem solved. For details on how this can be customized, see the Hibernate reference manual. Typically you would use

@ManyToOne
@JoinColumn(name = "addressId")
private Address address;
like image 127
JB Nizet Avatar answered Nov 07 '22 19:11

JB Nizet