初始SessionFactory创建failed.org.hibernate.MappingException:Composite-id类必须实现Serializable

时间:2016-12-05 01:40:21

标签: java hibernate

我有一个包含id的抽象类。它由实体类扩展。假设实体类的getter和setter。

@MappedSuperclass
public abstract class BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    protected long id;

    @Column(name = "created_on", nullable = false)
    private Timestamp createdOn;

    @Column(name = "last_update")
    private Timestamp lastUpdate;
}

@Entity
@Table(name = "OPPORTUNITY")
public class Opportunity extends BaseEntity {

    @Id
    @Column(name = "name")
    public String name;

    @Column(name = "phone")
    public String phone;

    @Column(name = "email")
    public String email;

    @Column(name = "message")
    public String message;
}

但是,我有以下异常:

Initial SessionFactory creation failed.org.hibernate.MappingException: Composite-id class must implement Serializable: com.inbhiwadi.model.Opportunity

java.lang.ExceptionInInitializerError
at    com.inbhiwadi.dao.hibernate.SessionFactoryProvider.buildSessionFactory(SessionFactoryProvider.java:35)
at com.inbhiwadi.dao.hibernate.SessionFactoryProvider.<clinit>(SessionFactoryProvider.java:18)
at com.inbhiwadi.dao.OpportunityDao.<init>(OpportunityDao.java:21)
at com.inbhiwadi.dao.OpportunityDaoTest.<init>(OpportunityDaoTest.java:17)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at
 com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.hibernate.MappingException: Composite-id class must implement Serializable: com.inbhiwadi.model.Opportunity
at org.hibernate.mapping.RootClass.checkCompositeIdentifier(RootClass.java:291)
at org.hibernate.mapping.RootClass.validate(RootClass.java:274)
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:482)
at org.hibernate.boot.internal.MetadataImpl.buildSessionFactory(MetadataImpl.java:170)
at com.inbhiwadi.dao.hibernate.SessionFactoryProvider.buildSessionFactory(SessionFactoryProvider.java:31)
... 29 more

对我来说很好看。不知道我到底错在哪里?

0 个答案:

没有答案