2012-11-11 103 views
1

如何用Hibernate映射我自己的类作为其他类中属性的类型?例如,我有类地址和类用户。我尝试映射如下:休眠。如何将自己的类映射为实体中的属性类型?

public class User { 
    private Long id; 
    private Address address; 
    // other fields 
} 

但我得到的异常在这种情况下:

org.hibernate.MappingException: Could not determine type for: es.myproject.entity.User 

我将是任何指导或相应的样本上有用的链接感谢。最好使用Hibernate注释。提前致谢!

回答

1

您需要添加说明两个实体之间关系的注释,例如@ManyToOne,@OneToOne@OneToMany

大概是这样的:

@Entity 
public class User { 
    @Id 
    private Long id; 

    @OneToOne(mappedBy="user") 
    private Address address; 
    // other fields 
}