Hibernate,主键包括自动生成的id和外键作为pk

时间:2013-09-28 22:04:54

标签: hibernate jpa

Welecome,我在休眠方面很新,我有问题。 上面我插入代码示例。  订单应该包括2个主键id(own.auto生成的pk)和其他pk(也作为来自Client表的fk)id的客户端的make命令。除了我添加@GeneratedValue(strategy=GenerationType.AUTO)之后,一切正常 我正在考虑错误:

org.hibernate.PropertyAccessException: could not set a field value by reflection setter of pl.pb.intern.test.model.Order.id
    at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:151)
    at org.hibernate.mapping.Component$ValueGenerationPlan.execute(Component.java:423)
    at org.hibernate.id.CompositeNestedGeneratedValueGenerator.generate(CompositeNestedGeneratedValueGenerator.java:121)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:735)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:727)
    at org.hibernate.engine.spi.CascadingAction$5.cascade(CascadingAction.java:258)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:388)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:331)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:209)
    at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:418)
    at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:358)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:334)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:209)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:166)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:132)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:364)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:338)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:241)
    at org.hibernate.event.internal.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireUpdate(SessionImpl.java:786)
    at org.hibernate.internal.SessionImpl.update(SessionImpl.java:778)
    at org.hibernate.internal.SessionImpl.update(SessionImpl.java:774)
    at pl.pb.intern.test.dao.HibernateClientDAO.updateClient(HibernateClientDAO.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)


Caused by: java.lang.IllegalArgumentException: Can not set long field pl.pb.intern.test.model.Order.id to org.hibernate.id.IdentifierGeneratorHelper$2
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeLongFieldAccessorImpl.set(Unknown Source)
    at java.lang.reflect.Field.set(Unknown Source)
    at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:139)
    ... 67 more

我的实体:

  @Entity
    public class Client
    {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    int id;

    @OnetoMany(mappedBy="client", cascade=CascadeType.ALL , fetch=FetchType.EAGER,orphanRemoval=true)
    List<Order> orders
    //other fields


    }

    @Enitity
    public classs Order
    {
        @Id
         @GeneratedValue(strategy=GenerationType.AUTO)
         int id;

         @Id
        @ManyToOne
        @JoinColumn( name="order_client_id",insertable=false, updatable=false)   
        Client client;

           public void setId(long id)
          {
           this.id=id
          }

          public login getId()
         {
           return this.id;
        }
     }



    }

我已经查看了hibernate docs,我没有找到任何禁止连接自动生成的主键和外键作为pk。在这个例子中出了什么问题??

1 个答案:

答案 0 :(得分:0)

关于复合主键问题,请参阅JPA composite primary key

从异常堆栈跟踪中,Order.getId() / Order.setId()的方法签名似乎有错误的签名。根据JavaBean约定,由于Order.idintsetId()应该采用int参数,而getId()应该相应地返回int。或者,您可以将Order.id的类型更改为long,因为getId() / setId()已使用long

此外,您可能希望编辑代码以修复以下拼写错误和语法错误: Client.java:      - @OnetoMany应该是@OneToMany      - List<Order> orders上缺少行尾分号 Order.java      - this.id = id

Order.setId上缺少行尾分号