gpt4 book ai didi

java - JPA - 在模型中使用注解

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:35:06 25 4
gpt4 key购买 nike

我的应用程序在 JPA/Hibernate、Spring 和 Wicket 上运行。我正在尝试将我们的 ORM 从 XML 文件转换为 JPA 注释。带注释的模型如下所示:

@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 中描述:

<?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 不会被创建,但有一个异常(exception)

org.hibernate.PropertyNotFoundException:
Could not find a setter for property
fullName in class
org.appfuse.model.User.

没有为此属性设置映射,因为它只是一个方便的方法。我做错了什么?

最佳答案

将方法标记为@Transient所以 Hibernate 会忽略它:

/**
* @return Returns firstName and lastName
*/
@Transient
public String getFullName() {
return firstName + ' ' + lastName;
}

默认情况下,所有看起来像 getter 的东西都会被映射。

关于java - JPA - 在模型中使用注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3891328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com