如何将注释用于标识我的实体与许多字段,但这不是ID字段(Hibernate)

时间:2012-04-23 07:54:31

标签: hibernate ejb-3.0

我正在使用Hibernate和EJB 3(没有xml)。 我需要实现一个逻辑来防止重复数据。我有这样的课程:

public class CvnCity  implements java.io.Serializable {

   private Integer idCity;
   private String city;

   public CvnCity() {
   }  

   public CvnCity(String city) {
      this.city = city;
   }

   @Id @GeneratedValue(strategy=IDENTITY)

   @Column(name="ID_CITY", unique=true, nullable=false)
   public Integer getIdCity() {
      return this.idCity;
   }
...

}

另一个像这样:

public class Person实现java.io.Serializable {

   private Integer idPerson;
   private String name;
   private City city;

   ...

}

我想阻止插入具有相同名称和同一城市的Person,我也需要将此问题扩展到许多类,所以我要实现一个只有输入的通用逻辑“uniques”字段。

1 个答案:

答案 0 :(得分:0)

查看NaturalIdHibernate Annotations ReferenceSection 2.2.3.2. Composite identifier