@generatedvalue不工作,仍然要求手动分配id

时间:2014-09-11 04:02:28

标签: spring hibernate hibernate-annotations

我正在学习spring-hibernate并试图让我的Web服务正常工作,但现在我已经陷入了这种状况。我的服务是为了让数据运行良好,但是当我试图发布这种情况时:

  1. 我已经使用了@generated value annotation
  2. 尝试修改它的类型如:GenerationType.SEQUENCE,IDENTITY等(它们都不起作用)
  3. 到目前为止,我最好的猜测,所以这是我的代码

    这是我的模型类(它的一部分),另一个是典型的电子邮件,电话等。它与另一个表没有任何关系,我的数据库上只有一个表,顺便说一句,我使用ms sql server

    @Entity
    @Table(name="Patient")
    @SuppressWarnings("serial")
    public class Patient implements Serializable {
    
        @Id
        @GenericGenerator(name = "guidGenerator", strategy = "guid")
        @GeneratedValue( generator= "guidGenerator")
        @Column(name = "PatientGUID", columnDefinition="uniqueidentifier")
        private String patientGuid;
        private String firstName;
        private String lastName;
    
        public String getPatientGuid() {
           return patientGuid;
        }
    
        public void setPatientGuid(String patientGuid) {
           this.patientGuid = patientGuid;
        }
    
        public void setFirstName(String firstName) {
           this.firstName = firstName;
        }
    
        public String getLastName() {
           return lastName;
        }
    
        public void setLastName(String lastName) {
           this.lastName = lastName;
        }
    }
    

    修改:我的hbm文件

     <hibernate-mapping package="training.model">
     <class name="Patient" table="Patient">     
        <id name="patientGuid" type="string">
            <column name="PatientGUID" />
            <generator class="assigned" />
        </id>
    
        <property name="practiceGuid" type="string">
            <column name="PracticeGUID" sql-type="varchar" length="38" not-null="true" />
        </property>
        <property name="firstName" type="string">
            <column name="FirstName" sql-type="varchar" length="30" not-null="true" />
        </property>
    
        <property name="lastName" type="string">
            <column name="LastName" sql-type="varchar" length="30" not-null="true" />
        </property>
    </class>
    </hibernate-mapping>
    

    我相信导致这个问题的表格细节是: PatientGUID ,它的类型是 UNIQUEIDENTIFIER ,当我执行我的webservice并请求POST方法发布我的病人时我得到了错误,即:

    在调用save()

    之前,必须手动分配

    此类的ID

    所以,我真的需要帮助,我做错了什么或者我错过了什么?如果您需要有关我的代码的更多详细信息,请发表评论,谢谢

    编辑:错误消息

     Caused by: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually              assigned before calling save(): training.model.Patient
    at org.hibernate.id.Assigned.generate(Assigned.java:52)
    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.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
    at training.dao.PatientDaoImpl.savePatient(PatientDaoImpl.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    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)
    at com.sun.proxy.$Proxy20.savePatient(Unknown Source)
    at training.service.PatientServiceImpl.addPatient(PatientServiceImpl.java:29)
    at training.ws.PatientWS.addPatients(PatientWS.java:110)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:180)
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
    

1 个答案:

答案 0 :(得分:0)

hibernate中没有guid策略,但有uuid。以下应该有效。

@GenericGenerator(name = "guidGenerator", strategy = "uuid2")