Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

orm

jpa

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.

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!)

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:

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?

like image 323
Skyhan Avatar asked Nov 07 '10 00:11

Skyhan


People also ask

How do you make a field non persistent?

Short answer is to use @Transient annotation.

What is the easiest way to ignore a JPA field during persistence?

To ignore a field, annotate it with @Transient so it will not be mapped by hibernate.

Which annotation in JPA may be used to specify whether enum should be persisted as number in database column?

The @Enumerated annotation enables you to define how an enum attribute gets persisted in the database. By default, all JPA implementations map the ordinal value of the enum to a numeric database column.


1 Answers

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

like image 98
Steven Benitez Avatar answered Sep 21 '22 13:09

Steven Benitez