Java 如何在 JPA 实体 bean 中使用或注释不应保留在数据库中的虚拟字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4115861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:46:46  来源:igfitidea点击:

How to use or annotate a dummy field in a JPA entity bean which is not supposed to be persisted in database

javaormjpa

提问by Skyhan

I have this code for login validation using a Struts2 action class which calls an EJB for LDAP validation and then if (LDAP credentials) validated, querying the user database to get the rest of the user information using the JPA entity bean which also acts like a POJO. Unlike the username, userid and other user info, password is not stored in the database, but for the sake of the POJO getter and setter method I attempt to include a dummy password field - for serving the Struts2 action form.

我有这个使用 Struts2 操作类进行登录验证的代码,它调用 EJB 进行 LDAP 验证,然后如果(LDAP 凭据)验证,查询用户数据库以使用 JPA 实体 bean 获取其余的用户信息,它也像一个POJO。与用户名、用户 ID 和其他用户信息不同,密码不存储在数据库中,但为了 POJO getter 和 setter 方法,我尝试包含一个虚拟密码字段 - 用于为 Struts2 操作表单提供服务。

The problem is after ldap authentication, an exception occurs stating that the column "password" does not exist in the database (which was never meant to be anyway!)

问题是在 ldap 身份验证后,发生异常,指出数据库中不存在列“密码”(无论如何,这绝不是!)

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'PASSWORD' in 'field list'
Error Code: 1054
Call: SELECT PERSON_ID, ACTIVE_USER, EMAIL, FIRSTNAME, SURNAME, PASSWORD, FULLNAME, EMPLOYEE_NUMBER FROM xxte_employees WHERE (ACTIVE_USER = ?)
        bind => [john.doe]
Query: ReadAllQuery(name="XxteEmployees.validateLogin" referenceClass=XxteEmployees sql="SELECT PERSON_ID, ACTIVE_USER, EMAIL, FIRSTNAME, SURNAME, PASSWORD, FULLNAME, EMPLOYEE_NUMBER FROM xxte_employees WHERE (ACTIVE_USER = ?)")
        at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
        at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:687)
        at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:530)
        at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:529)
....

Here's the code from the entity bean:

这是实体 bean 的代码:

public class XxteEmployees implements Serializable {
    private static final long serialVersionUID = 1L;
    @Column(name = "ACTIVE_USER")
    private String activeUser;
    @Column(name = "EMAIL")
    private String email;
    @Id
    @Basic(optional = false)
    @Column(name = "PERSON_ID")
    private Double personId;
    @Column(name = "EMPLOYEE_NUMBER")
    private Double employeeNumber;
    @Column(name = "FIRSTNAME")
    private String firstname;
    @Column(name = "SURNAME")
    private String surname;

    private String fullname;

    private String password;

    public XxteEmployees() {
    }

    public XxteEmployees(Double personId) {
        this.personId = personId;
    }

    public String getActiveUser() {
        return activeUser;
    }

    public void setActiveUser(String activeUser) {
        this.activeUser = activeUser;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Double getPersonId() {
        return personId;
    }

    public void setPersonId(Double personId) {
        this.personId = personId;
    }

    public Double getEmployeeNumber() {
        return employeeNumber;
    }

    public void setEmployeeNumber(Double employeeNumber) {
        this.employeeNumber = employeeNumber;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }


    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    // BEGIN: objects not in db

    public String getFullname() {
        return firstname + ' ' + surname;
    }


    public void setFullname(String fullname) {
        this.fullname = firstname + ' ' + surname;;
    }

    public String getPassword() {
        return sifre;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    // END: objects not in db

Any workaround for this?

有什么解决方法吗?

采纳答案by Steven Benitez

Annotate any fields that you don't want to be persisted as @Transient. It is in the javax.persistencepackage.

注释您不想保留为@Transient. 它在javax.persistence包裹里。