JPA - 在模型中使用注释 [英] JPA - using annotations in a model

查看:29
本文介绍了JPA - 在模型中使用注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在 JPA/Hibernate、Spring 和 Wicket 上运行.我正在尝试将我们的 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:找不到属性的 setter班级全名org.appfuse.model.User.

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

There's no mapping set for this property, because it's just a convenience method. What do I do wrong?

推荐答案

将方法标记为 @Transient 所以 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆