简体   繁体   中英

Hibernate - map one pojo to another pojo one poroperty

I have two entities

 @Entity
 @Table(name = "person")
Class Person {
@Id
@Column(name = "id")
private String id ;
private String addressId;
private String name ; 
// gets etc...}

and

@Entity
@Table(name = "address")
 Class Address {
@Id
@Column(name = "id")      
  String id ; 
  String personId ; 
  String streetId ; 
  String country ;
 // gets etc... } 

let's assume for my need that address have lots of persons and each person has only one address I would like to add to Person the property country alone how can i achive it ?

in the end I would like to have one pojo

@Entity
 @Table(name = "person")
class PersonWithCountry{
    @Id
@Column(name = "id")
private String id ;
@Column(name = "name")
private String name ; 
@Column(name = "what to write here")
private String country ; 

//get set }

}

Use JPA annotations:

In your Person class

    @ManyToOne(targetEntity = Address.class)
    @JoinColumn(name = "id")
    private Address address;

In your Address class

@OneToMany(mappedBy = "address")
private List<Person> persons;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM