2017-03-08 76 views
0

對於使用表映射的持久對象。映射類應該是POJO,具有Plain Old Java Object的所有規則。它的setter和getter不應該有任何其他的對象初始化代碼。休眠使用普通POJO

public class Cat { 

      String id; 
      String name; 
      Type type; 


     /** 
     * @return the id 
     */ 
     public String getId() { 
      return id; 
     } 
     /** 
     * @param id the id to set 
     */ 
     public void setId(String id) { 
      if(id != null) { 
       type = new Type(); //This will cause in exception org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update 
            //ERROR 2017-03-07 00:37:30,253 [tomcat-http--22] - ERROR: cannot execute UPDATE in a read-only transaction 
      } 
      this.id = id; 
     } 
     /** 
     * @return the name 
     */ 
     public String getName() { 
      return name; 
     } 
     /** 
     * @param name the name to set 
     */ 
     public void setName(String name) { 
      this.name = name; 
     } 

的例外是: -

WARN 2017-03-07 00:37:30,253 [tomcat-http--22] - SQL Error: 0, SQLState: 25006 
ERROR 2017-03-07 00:37:30,253 [tomcat-http--22] - ERROR: cannot execute UPDATE in a read-only transaction 
ERROR 2017-03-07 00:37:30,254 [tomcat-http--22] - Could not synchronize database state with session 
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update 
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168) 
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) 
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) 
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) 

就是爲什麼Hibernate只期望POJO嚴格的規則?冬眠有任何指導。

它的工作,如果我從方法setId中刪除代碼,只保留我的任務。

+0

這只是顯示愚蠢和不易伸縮的Hibernate如何能在倍。 (異常消息只是沒有任何意義) –

+0

你能提供你的代碼樣本嗎? – Reborn

回答

1

看起來你是在什麼地方標記方法@Transactional(只讀=真)

+0

這是一個簡單的pojo類。我沒有使用任何註釋。在映射文件中,什麼都不是隻讀的 –