10

During deploying my app I occurs on that exception. I have a lot of class in my app and I dont know where I must to place @IdClass and what does this exception mean anyway. I am using Hibernate 4.1 and 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

5 Answers5

18

@IdClass annotation is used to define the Class that contains the id. i.e. This is generally used in case defining a compound key. i.e. a key composite of more than one attribute. If that is the case, than this is how we do. take a look at following example.. we define a class as IdClass and use @Id's to define varrious Ids for thisIdClass`.

Example :

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

  @Id
  @ManyToOne  
  private Role roleId;

  private Date dateAssigned;
}

Hope this helps.

Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
8

Elaborating further on @Mukul correct answer the @IdClass should have the following properties:

  1. It should implement Serializable
  2. It should have a constructor that takes the ids (i.e. the fields associated with @Id)
  3. It should override equals and hashCode

It's sometime useful to make this class as a static inner class of the entity, here's an example:

@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);
        }
   }
}
Ithar
  • 4,865
  • 4
  • 39
  • 40
  • in my case i tried the same way u did but red mark is showing in AssignedRoleId.class – ashwin karki Nov 16 '18 at 06:04
  • @ashwinkarki am not sure why that would be the case. It's an inner class hence will be in the same package. Can you elaborate more as to why there are red marks. Suggestion would be to remove all annotations and setup a basic inner class. – Ithar Nov 16 '18 at 11:12
  • does this apply also if the IDs are string and int and not beans? – user666 Aug 26 '19 at 08:49
  • @user666 I would think it should apply to all classes that require a composite key. – Ithar Sep 02 '19 at 11:58
  • For my case I created a bean holding the two string and int variables – user666 Sep 03 '19 at 07:39
0

you can only overwrite equals and hashcode and the problem will be solved, and also need implement java.io.Serializable

Arman
  • 1
  • 1
0

I had the same problem. Turns out I wasn't using Hibernate correctly (no sh!t Sherlock). My configuration file hibernate.cfg.xml didn't include my Entity Java Classes... I am only using the hibernate.cfg.xml file for the hibernate configuration and only javax.persistence annotations for my Entity Classes.

I just added the missing Entities/Classes (4 Classes all in all) at the bottom of my configuration file and ran my test code and everything worked .

New 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>
0

I faced this error when I was extending from another entity and was also using @EmbeddedId. It was taking @Id from the parent entity. So having @Id and @EmbeddedId in the same entity hierarchy caused this for me. The error message was a little misguiding in my case. I got rid of the error by not extending from the parent.

This is not applicable for your case as you are not extending but I am writing this answer for people getting this error for an issue which is different from what mentioned in the error message.

Praveen Kumar
  • 222
  • 3
  • 6