【发布时间】:2016-10-18 01:54:21
【问题描述】:
我有一个使用注释的工作休眠设置
@Entity
@Table(name="Users")
public class User implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="USER_ID")
private int id = 0;
@Column(name="EMAIL")
private String email = "";
@Temporal(TemporalType.TIMESTAMP)
@Column(name="CREATED")
private Date created = null;
public User(){
Calendar cal = Calendar.getInstance();
this.created = cal.getTime();
}
public User(int id, String email, Date created) {
this.id = id;
this.email = email;
this.created = created;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
@Override
public String toString() {
return "centaurus.hib.User{" +
"id=" + id +
", email='" + email + '\'' +
", created=" + created +
'}';
}
}
要完成这项工作,我必须在我的 hibernate.cfg.xml 文件中有一个条目(以及其他条目)
<mapping class="centaurus.hib.User"/>
否则休眠会抛出错误,说它没有映射文件。
或者,当我创建持久会话工厂时,我可以指定要映射的类。
问题是,在我从事过的其他项目中,我只需要添加一个类并正确注释它,hibernate 就可以使用它。这是我想做但不知道如何做的。除了带注释的类之外,我不想在我的休眠配置中有一个类列表。
【问题讨论】:
-
您可以在您的休眠属性中使用注释驱动,并对您的类包进行组件扫描,然后您只需使用一对一、一对多等映射来驱动您的类。
-
你试过纯JPA方式吗?不是休眠特定的东西,只能使用persistence.xml?