2016-10-13 2 views
0

Я пытался использовать AES с аннотацией hibernate columnTransformer, но дает мне некоторые ошибки.Hibernate @ColumnTransformer (read = "...", write = "...") дает некоторые ошибки, как я могу его решить?


  • база данных была сгенерирована с Hibernate.
  • Появляется только при попытке использовать AES для чтения и записи параметров на @ org.hibernate.annotations.ColumnTransformer.

Мои Employeeclass

import java.io.Serializable; 

import javax.persistence.Id; 
import javax.persistence.Table; 
import javax.persistence.Column; 
import javax.persistence.Entity; 

@Entity() 
@Table(name = "EMPLOYEE") 
public class Employee implements Serializable { 

    private static final long serialVesionUID = 1L; 

    @Id 
    private Integer employeeId; 

    @org.hibernate.annotations.NaturalId 
    private String userName; 

    @Column(name = "password") 
    @org.hibernate.annotations.ColumnTransformer(
     read = "decrypt('AES', '00', password)", 
     write = "encrypt('AES', '00', ?)" 
    ) 
    private String password; 

    private int accessLevel; 

    public Employee(Integer employeeId, String userName, String password, int accessLevel) { 
     this.employeeId = employeeId; 
     this.userName = userName; 
     this.password = password; 
     this.accessLevel = accessLevel; 
    } 

    //Getters and Setters 

} 

Мой Главный класс

import javax.persistence.Persistence; 
import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import hibernate.examples.columntransformers.readandwriteexpressions.Employee; 

public class App { 

    private static EntityManagerFactory emf; 
    private static EntityManager em; 

    public static void main(String[] args) { 

     emf = Persistence.createEntityManagerFactory("LAVM"); 
     em = emf.createEntityManager(); 

     try { 

      em.getTransaction().begin();    

      Employee emp = new Employee(123, "someUserName", "somePassword", 1); 

      em.persist(emp); 
      em.getTransaction().commit(); 

     } catch (Exception e) {     
      e.printStackTrace(); 
     } finally { 
      em.close(); 
      emf.close(); 
     } 
    } 
} 

дает мне эти ошибки

javax.persistence.RollbackException: Error while committing the transaction 
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:94) 
at hibernate.examples.main.App.main(App.java:38) 
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement 
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763) 
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677) 
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:82) 
... 1 more 


Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement 
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80) 
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) 
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) 
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:190) 
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62) 
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124) 
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3587) 
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:103) 
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:453) 
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345) 
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) 
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) 
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218) 
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:421) 
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) 
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177) 
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77) 
... 1 more 


Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Incorrect parameter count in the call to native function 'encrypt' 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
at com.mysql.jdbc.Util.getInstance(Util.java:386) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794) 
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359) 
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187) 
... 14 more 


oct 13, 2016 11:08:31 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop 
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate] 

ответ

0

У вас есть исключение, связанное с Неправильными числом параметров в вызове функции нативного encrypt.

Проверить количество параметров для decrypt и encrypt функций, я предполагаю, что это должно быть два и не три, как вы использовали для столбца паролей.