Java – JPA – using annotations in the model

My application runs on JPA / Hibernate, spring and wicket I'm trying to convert our ORM from XML file to JPA annotation The annotated model is as follows:

@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;
    }
}

Initially, it has no comments, and it's in user hbm. The mapping is described in 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>

When I delete the mapping file and try to use annotations only, the entitymanagerfactory is not created, but an exception occurs

This property has no mapping set because it is just a convenient method What did I do wrong?

Solution

Mark the method @ transient so hibernate ignores it:

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

By default, everything that looks like a getter is mapped

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>