Java Hibernate使用普通POJO

Java Hibernate使用普通POJO,java,hibernate,javabeans,pojo,Java,Hibernate,Javabeans,Pojo,用于与表映射的持久对象。映射类应该是POJO,具有普通旧Java对象的所有规则。它的setter和getter不应该有任何其他初始化对象的代码 public class Cat { String id; String name; Type type; /** * @return the id */ public String getId() {

用于与表映射的持久对象。映射类应该是POJO,具有普通旧Java对象的所有规则。它的setter和getter不应该有任何其他初始化对象的代码

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?hibernate对此有什么指导方针吗


如果我从方法setId中删除代码,只保留I'd的赋值,它就可以工作。

看起来您在某个地方将方法标记为@Transactional(readonly=true)

这正好说明Hibernate有时是多么愚蠢和不灵活。(异常消息没有任何意义)您能提供您的代码示例吗?它是一个简单的pojo类。我没有使用任何注释。在映射文件中,没有任何内容是只读的