2012-10-17 63 views
1

在部署我的應用程序期間,我發生該異常。我在我的應用程序中有很多課程,我不知道我必須在哪裏放置@IdClass,無論如何,這個例外意味着什麼。 我使用Hibernate的4.1和JBoss AS 7.1IllegalArgumentException:期待IdClass映射

12:10:23,761 INFO [org.hibernate.engine.jdbc.internal.LobCreatorBuilder] (MSC service thread 1-5) HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException 
12:10:24,075 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (MSC service thread 1-5) HHH000389: Unsuccessful: drop sequence hibernate_sequence 
12:10:24,076 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (MSC service thread 1-5) ERROR: sequence "hibernate_sequence" does not exist 
12:10:24,281 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-5) MSC00001: Failed to start service jboss.persistenceunit."kladr.ear/kladr-ejb-1.0-SNAPSHOT.jar#primary": org.jboss.msc.service.StartException in service jboss.persistenceunit."kladr.ear/kladr-ejb-1.0-SNAPSHOT.jar#primary": Failed to start service 
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1767) [jboss-msc-1.0.2.GA.jar:1.0.2.GA] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_35] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_35] 
    at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_35] 
Caused by: java.lang.IllegalArgumentException: expecting IdClass mapping 
    at org.hibernate.ejb.metamodel.AttributeFactory$3.resolveMember(AttributeFactory.java:878) 
    at org.hibernate.ejb.metamodel.AttributeFactory$4.resolveMember(AttributeFactory.java:915) 
    at org.hibernate.ejb.metamodel.AttributeFactory.determineAttributeMetadata(AttributeFactory.java:423) 
    at org.hibernate.ejb.metamodel.AttributeFactory.buildAttribute(AttributeFactory.java:91) 
    at org.hibernate.ejb.metamodel.MetadataContext.wrapUp(MetadataContext.java:214) 
    at org.hibernate.ejb.metamodel.MetamodelImpl.buildMetamodel(MetamodelImpl.java:64) 
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:91) 
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904) 
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889) 
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73) 
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162) 
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85) 
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA] 
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA] 
    ... 3 more 

回答

6

@IdClass註解被用來定義一個包含ID類。即這在定義複合密鑰的情況下通常使用。即多於一個屬性的關鍵組合。 如果是這樣,那麼我們就是這樣做的。看看下面的例子..我們定義一個類爲IdClass,並使用@Id's來定義各種Id IdIdClass`。

例子:

@Entity 
@IdClass(AssignedRoleId.class) 
public class AssignedRole 
{ 
    @Id 
    @ManyToOne 
    private User userId; 

    @Id 
    @ManyToOne 
    private Role roleId; 

    private Date dateAssigned; 
} 

希望這有助於。

+1

是的。 Tnx很多。問題是我班上有多個ID,這是我的錯! –

1

上@Mukul正確答案@IdClass應具有以下特性進一步闡述:

  1. 應該實現Serializable
  2. 它應該有一個構造函數的ID(即@Id相關的字段)
  3. 它應該重寫equalshashCode

這一段時間有用的做這個類作爲實體的靜態內部類,這裏是一個例子:

@Entity 
@IdClass(AssignedRoleId.class) 
public class AssignedRole 
{ 
    @Id 
    @ManyToOne 
    private User userId; 

    @Id 
    @ManyToOne 
    private Role roleId; 

    private Date dateAssigned; 

    public static class AssignedRoleId implements Serializable { 

     private User userId; 
     private Role roleId; 

     public AssignedRoleId() {} 

     public AssignedRoleId(User userId, Role roleId) { 
      this.userId = userId; 
      this.roleId = roleId; 
     } 

     @Override 
     public boolean equals(Object o) { 

      if (o == this) { 
       return true; 
      } 
      if (!(o instanceof AssignedRole)) { 
       return false; 
      } 
      AssignedRole assignedRole = (AssignedRole) o; 
      return Objects.equals(userId, assignedRole.getUserId()) && 
        Objects.equals(roleId, assignedRole.getRoleId()); 
     } 

     @Override 
     public int hashCode() { 
      return Objects.hash(userId, roleId); 
     } 
    } 
} 
相關問題