java - Hibernate 使用普通 POJO

标签 java hibernate javabeans pojo

对于与表映射的持久对象。映射类应该是具有普通旧 Java 对象所有规则的 POJO。它的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;
        }

异常(exception)是:-

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 中删除代码,则它可以工作,只保留 Id 的分配。

最佳答案

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

关于java - Hibernate 使用普通 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42667257/

相关文章:

java - 根据内容检测文件类型

java - Hibernate使用PostgreSQL序列不影响序列表

java - Hibernate 抛出的 MultipleBagFetchException

spring-boot - Spring Boot 依赖注入(inject)如何使用不同类型的注解

java - Spring - 仅当值不为空时才设置属性

java - 我们可以避免java中字符串的实习吗?

java - Java 中令人费解的数组赋值行为

java - 使用 Robolectric 测试 Android 库

java - 在没有静态测试数据库的情况下,如何使 DAO 类的单元测试不那么脆弱?

java - 在JSP中使用JavaBean时出现异常