【问题标题】:Hibernate JPA Persist休眠 JPA 持久化
【发布时间】:2014-09-21 11:27:27
【问题描述】:

我有一个实体类。当我坚持时,我收到错误:“无法通过反射 getter 获取字段值”。

请帮帮我。

我的实体类代码:



    @Entity
    @Table( name = "class" )
    @NamedQueries( {
        @NamedQuery( name = "SchoolClass.findAll", query = "SELECT c FROM SchoolClass c" ),
        @NamedQuery( name = "SchoolClass.findById", query = "SELECT c FROM SchoolClass c WHERE c.id = :id" )
    } )
    public class SchoolClass implements Serializable {

        /**
         * Generated Serial Version ID
         */
        private static final long   serialVersionUID    = 6703663444336018288L;

        @Id
        @Column( nullable = false, updatable = false )
        private Long                id;

        @Column( nullable = false, length = 100 )
        private String              name;

        /**
         * @return The getter method of the 'id' instance variable
         */
        public Long getId() {
            return id;
        }

        /**
         * @param The setter method of the 'id' instance variable
         */
        public void setId( final Long id ) {
            this.id = id;
        }

        /**
         * @return The getter method of the 'name' instance variable
         */
        public String getName() {
            return name;
        }

        /**
         * @param The setter method of the 'name' instance variable
         */
        public void setName( final String name ) {
            this.name = name;
        }

        /* (non-Javadoc)
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
            return "SchoolClass [id=" + id + ", name=" + name + "]";
        }
    }

我的持久化代码:



    try {
            final UserTransaction transaction = ( UserTransaction ) new InitialContext()
                .lookup( "java:comp/UserTransaction" );

            transaction.begin();

            entityManager.persist( schoolClass ); // Exception here
            entityManager.flush();

            transaction.commit();

        } catch ( SecurityException | IllegalStateException | NamingException | NotSupportedException | SystemException
            | RollbackException | HeuristicMixedException | HeuristicRollbackException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

我的例外:

原因:javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: 无法通过 com.ilkerkonar.applications.schoolproject.orm.model.SchoolClass.id 的反射 getter 获取字段值 在 org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763) 在 org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677) 在 org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683) 在 org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1187) 在 com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:287) 在 com.ilkerkonar.applications.schoolproject.orm.service.ClassService.addNewClass(ClassService.java:61)

【问题讨论】:

    标签: java hibernate jpa


    【解决方案1】:

    您是否也尝试过添加@GeneratedValue?您使 id 不可更新,而无需告诉 JPA 如何首先生成 id。它也应该通过删除 updatable=false 来工作。

    【讨论】:

    • 是的,我已经尝试过@GeneratedValue,并且还删除了 updatable=false。但它们都不起作用。我得到同样的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    相关资源
    最近更新 更多