在部署我的应用程序期间,我发生了该异常。我的应用程序中有很多课程,我不知道在哪里放置@IdClass
,这个例外意味着什么。
我正在使用Hibernate 4.1和JBoss AS 7.1
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
答案 0 :(得分:11)
@IdClass
注释用于定义包含id的Class。即,这通常用于定义复合键的情况。即一个以上属性的关键复合。
如果是这样的话,那就是我们这样做的方式。看一下下面的例子..我们将一个类定义为IdClass
并使用@Id
来定义不同的Id s for this
IdClass`。
示例:
@Entity
@IdClass(AssignedRoleId.class)
public class AssignedRole
{
@Id
@ManyToOne
private User userId;
@Id
@ManyToOne
private Role roleId;
private Date dateAssigned;
}
希望这会有所帮助。
答案 1 :(得分:3)
进一步阐述@Mukul正确答案@IdClass
应该具有以下属性:
@Id
相关联的字段)equals
和hashCode
将此类作为实体的静态内部类有时非常有用,这是一个示例:
@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);
}
}
}
答案 2 :(得分:0)
您只能覆盖equals和hashcode,问题将得以解决,并且还需要实现java.io.Serializable
答案 3 :(得分:0)
我遇到了同样的问题。原来我没有正确使用 Hibernate(没有 sh!t Sherlock)。我的配置文件 hibernate.cfg.xml
不包含我的实体 Java 类...我只使用 hibernate.cfg.xml
文件进行休眠配置,并且只使用 javax.persistence
注释用于我的实体类。
我刚刚在配置文件的底部添加了缺少的实体/类(总共 4 个类)并运行了我的测试代码,一切正常。
新hibernate.cfg.xml
:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.username">foo</property>
<property name="connection.password">bar</property>
<mapping class="test.entities.MixEntity"/>
<mapping class="test.entities.ZutatEntity"/>
<mapping class="test.entities.RezeptEntity"/>
<mapping class="test.entities.MixEntityPK"/>
</session-factory>
</hibernate-configuration>