JPA - 使用注释在模型 [英] JPA - using annotations in a model
问题描述
我的应用程序在JPA / Hibernate中,Spring和检票运行。我想我们的ORM从XML文件转换为JPA注解。带注释的模式是这样的:
My application runs on JPA/Hibernate, Spring and Wicket. I'm trying to convert our ORM from XML files to JPA annotations. The annotated model looks like this:
@Entity
@Table(name = "APP_USER")
public class User extends BaseObject {
private Long id;
private String firstName;
private String lastName;
private String email;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="FIRST_NAME")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name="LAST_NAME")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name="EMAIL")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
/**
* @return Returns firstName and lastName
*/
public String getFullName() {
return firstName + ' ' + lastName;
}
}
原来,虽然,它是没有标注,并在User.hbm.xml被描述的映射:
Originally, though, it was without annotation and the mapping was described in User.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.appfuse.model.User" table="app_user">
<id name="id" column="id" unsaved-value="null">
<generator class="increment"/>
</id>
<property name="firstName" column="first_name" not-null="true"/>
<property name="lastName" column="last_name" not-null="true"/>
<property name="email" column="email"/>
</class>
</hibernate-mapping>
当我删除映射文件,并尝试使用刚刚注释中,EntityManagerFactory还没有生成,唯独
When I delete the mapping file and try to use just the annotations, the entityManagerFactory doesn't get created, with the exception
org.hibernate.PropertyNotFoundException:
找不到属性的设置
全名类
org.appfuse.model.User。
有没有映射设置此属性,因为它只是一个方便的方法。我该怎么办错了?
There's no mapping set for this property, because it's just a convenience method. What do I do wrong?
推荐答案
标记方法,<一个href=\"http://download.oracle.com/javaee/5/api/javax/persistence/Transient.html\"><$c$c>@Transient$c$c>所以Hibernate会忽略它:
Mark the method as @Transient
so Hibernate will ignore it:
/**
* @return Returns firstName and lastName
*/
@Transient
public String getFullName() {
return firstName + ' ' + lastName;
}
在默认情况下,一切都看起来像一个getter映射。
By default, everything that looks like a getter is mapped.
这篇关于JPA - 使用注释在模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!