Hibernate - 将一个pojo映射到另一个pojo一个属性

时间:2014-08-13 09:22:26

标签: java hibernate jpa orm

我有两个实体

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

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

让我们假设我需要这个地址有很多人,每个人只有一个地址 我想加入Person 仅财产国家  我怎么能得到它?

最后我想要一个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 ; 

//得到设定 }

}

1 个答案:

答案 0 :(得分:0)

使用JPA注释:

Person班级

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

Address班级

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