简体   繁体   English

Hibernate JPA坚持

[英]Hibernate JPA Persist

I have an entity class. 我有一个实体类。 When I persist, I get the error: "could not get a field value by reflection getter of". 当我坚持执行时,出现错误:“无法通过反射吸气剂获得字段值”。

Please help me. 请帮我。

My entity class code: 我的实体类代码:



    @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 + "]";
        }
    }

My persistence code: 我的持久性代码:



    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();
        }

My exception: 我的例外:

Caused by: javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.ilkerkonar.applications.schoolproject.orm.model.SchoolClass.id at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1187) at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:287) at com.ilkerkonar.applications.schoolproject.orm.service.ClassService.addNewClass(ClassService.java:61) 由以下原因引起:javax.persistence.PersistenceException:org.hibernate.PropertyAccessException:无法通过org.hibernate.jpa.spi.AbstractEntityManagerImpl上的com.ilkerkonar.applications.schoolproject.orm.model.SchoolClass.id的反射getter获取字段值。 org.hibernate.jpa.spi上的.convert(AbstractEntityManagerImpl.java:1763)org.org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)上的org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)上的。 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)

have you tried adding @GeneratedValue as well? 您是否也尝试过添加@GeneratedValue You make the id non up-datable, w/o telling JPA how to generate the id in the first place. 使ID不可更新,首先不告诉JPA如何生成ID。 It should also work by removing updatable=false . 它也应该通过删除updatable=false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM