-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Help with Embedded Composite primary key problem!
PostPosted: Thu Aug 14, 2008 3:38 pm 
Newbie

Joined: Mon Jan 28, 2008 1:50 pm
Posts: 9
Location: EMC
Hi all,

I am having a problem with my embedded key. The objects appear to be properly inserted, but I am getting an exception when running a basic query. Here are the relevant snippets of code. Please help!!!!!

Code:
@Entity
@Table(name="IMFPRIVILEGES")
public class Privilege implements Serializable{
   @EmbeddedId
   private Privilege.PK pk;

   private String description;

   @ManyToOne(fetch=FetchType.EAGER,
         cascade = CascadeType.REFRESH)
                   @JoinColumn(name="COMPONENTUID", insertable=false)
   private VAApp component;

   @ManyToMany(fetch=FetchType.EAGER,
         cascade= CascadeType.ALL
   )
   @JoinTable(
         name="ROLE_PRIVILEGE",
         joinColumns={@JoinColumn(name="PRIVILEGEKEY"),
                   
                       @JoinColumn(name="COMPONENTUID")},
         inverseJoinColumns={@JoinColumn(name="ROLEID")}
         
   )
   private Set<Role> roles;

Privilege(String privilegekey, String description, VAApp vaapp) {
      super();
      this.component = vaapp;
      PK pk = new PK();
      pk.setComponentuid(vaapp.getComponentid());
      pk.setPrivilegekey(privilegekey);
      this.pk = pk;
      roles = new HashSet<Role>();
   }
   

   Privilege(){
      
   }

   @Embeddable
   public static class PK implements Serializable {
      private String privilegekey;
      private long componentuid;

      public PK() {
         super();
      }
               public void setComponentuid(long componentuid) {
         this.componentuid = componentuid;
      }
                public void setPrivilegekey(String privilegekey) {
         this.privilegekey = privilegekey;
      }
       }
}
   


@Entity
@DiscriminatorValue("VAApp")
public class VAApp extends Component implements VAAppIf {
   
   @OneToMany(mappedBy="componentid",
         cascade=CascadeType.ALL,
         fetch=FetchType.EAGER)
   private Set<Role> roles;

   @OneToMany(mappedBy="component",
         cascade = {   CascadeType.REFRESH,
                  CascadeType.REMOVE,
                  CascadeType.PERSIST
            },
         fetch=FetchType.EAGER)
   private Set<Privilege> privileges;
}

@Entity
@Table(name="ROLES")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="roleinheritancetype",
    discriminatorType=DiscriminatorType.STRING
)
@DiscriminatorValue("BASEROLE")

class Role implements Serializable, RoleIf {
   
   @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
   private long roleid;

   private boolean isapprole;

   private String description;
   
   private String rolename;
   
   @Version
   @Column(name = "OBJ_VERSION")
   private int obj_version;

   @ManyToOne(targetEntity=VAApp.class,
         fetch=FetchType.EAGER,
         cascade = {CascadeType.REFRESH}
   )
   @JoinColumn(name="COMPONENTID")
   private VAApp componentid;
   
   @ManyToMany(
         targetEntity=com.emc.imf.core.model.Privilege.class,
         fetch=FetchType.EAGER ,
         cascade={CascadeType.REFRESH}
      )
   @JoinTable(name="ROLE_PRIVILEGE",   
       inverseJoinColumns={@JoinColumn(name="PRIVILEGEKEY"),
                   JoinColumn(name="COMPONENTUID")},
         joinColumns={@JoinColumn(name="ROLEID")}
   )
   private Set<Privilege> privileges;
}


When I try to retrieve a "VAApp" with a really simple:

Code:
em.find(VAApp.class, appid);

I get the following exception:


javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not initialize a collection: [com.emc.imf.core.model.Privilege.roles#component[componentuid,privilegekey]{componentuid=13, privilegekey=PrivForIMF_9}]
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:637)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:74)
at com.emc.imf.core.model.DbObjRetriever.getVAApps(DbObjRetriever.java:34)
at com.emc.imf.core.model.BOMFacadeManager.getVAApps(BOMFacadeManager.java:463)
at com.emc.imf.core.model.TestComponentAPI.removeAllApps(TestComponentAPI.java:673)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.exception.DataException: could not initialize a collection: [com.emc.imf.core.model.Privilege.roles#component[componentuid,privilegekey]{componentuid=13, privilegekey=PrivForIMF_9}]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:77)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2001)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:63)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:454)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:844)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:241)
at org.hibernate.loader.Loader.doList(Loader.java:2213)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:65)
... 25 more
Caused by: SQL Exception: Invalid character string format for type INTEGER.
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedResultSet.noStateChangeException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.setString(Unknown Source)
at org.hibernate.type.StringType.set(StringType.java:26)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)
at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:284)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1707)
at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1678)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1563)
at org.hibernate.loader.Loader.doQuery(Loader.java:673)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1994)

I realize that the root of the problem looks like: Caused by: SQL Exception: Invalid character string format for type INTEGER.

Here is the details from the debug log of the query that was generated:

14 Aug 2008 13:08:51 DEBUG [main] def.DefaultInitializeCollectionEventListener - collection not cached
14 Aug 2008 13:08:51 DEBUG [main] loader.Loader - loading collection: [com.emc.imf.core.model.Privilege.roles#component[componentuid,privilegekey]{componentuid=13, privilegekey=PrivForIMF_9}]
14 Aug 2008 13:08:51 DEBUG [main] jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
14 Aug 2008 13:08:51 DEBUG [main] jdbc.ConnectionManager - opening JDBC connection
14 Aug 2008 13:08:51 DEBUG [main] connection.DriverManagerConnectionProvider - total checked-out connections: 0
14 Aug 2008 13:08:51 DEBUG [main] connection.DriverManagerConnectionProvider - using pooled JDBC connection, pool size: 0
14 Aug 2008 13:08:51 DEBUG [main] jdbc.AbstractBatcher - select roles0_.PRIVILEGEKEY as PRIVILEG2_2_, roles0_.COMPONENTUID as COMPONEN3_2_, roles0_.ROLEID as ROLEID2_, role1_.roleid as roleid105_0_, role1_.COMPONENTID as COMPONEN8_105_0_, role1_.description as descript3_105_0_, role1_.isapprole as isapprole105_0_, role1_.OBJ_VERSION as OBJ5_105_0_, role1_.rolename as rolename105_0_, role1_.ROLETYPE as ROLETYPE105_0_, role1_.roleinheritancetype as roleinhe1_105_0_, vaapp2_.COMPONENTID as COMPONEN2_102_1_, vaapp2_.NAME as NAME102_1_, vaapp2_.OBJ_VERSION as OBJ4_102_1_, vaapp2_.PATHNAME as PATHNAME102_1_, vaapp2_.APPVERSION as APPVERSION102_1_ from ROLE_PRIVILEGE roles0_ left outer join ROLES role1_ on roles0_.ROLEID=role1_.roleid left outer join IMFCOMPONENT vaapp2_ on role1_.COMPONENTID=vaapp2_.COMPONENTID where roles0_.PRIVILEGEKEY=? and roles0_.COMPONENTUID=?
14 Aug 2008 13:08:51 DEBUG [main] jdbc.AbstractBatcher - preparing statement
14 Aug 2008 13:08:51 DEBUG [main] type.NullableType - binding '13' to parameter: 1
14 Aug 2008 13:08:51 DEBUG [main] type.NullableType - binding 'PrivForIMF_9' to parameter: 2
14 Aug 2008 13:08:51 INFO [main] type.NullableType - could not bind value 'PrivForIMF_9' to parameter: 2; Invalid character string format for type INTEGER.
14 Aug 2008 13:08:51 DEBUG [main] jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
14 Aug 2008 13:08:51 DEBUG [main] jdbc.AbstractBatcher - closing statement
14 Aug 2008 13:08:51 DEBUG [main] jdbc.ConnectionManager - aggressively releasing JDBC connection


Just getting just the privileges with the call below generates the exact same error, but that is probably because I am doing eager loading, so the problem is coming from the loading of the VAApp object. . .

Code:
   Query query = em.createQuery("from Privilege");
   query.setHint("org.hibernate.readOnly", true);
   List<Privilege> privileges = query.getResultList();



But I don't understand why, as the ddl for the database is has componentuid as a integer and privilegekey as a string. It is also interesting to note, that this worked fine under 3.3.0 GA of the EntityManager.

Thanks for you help!!!!!

Shannon
Code:

_________________
Shannon Moyes Clark


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 20, 2008 9:21 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

just a guess, but have to tried to switch the order of @JoinColumn within the @JoinTable ROLE_PRIVILEGE. The order is important and it seems to me looking at the SQL that it might be inverted in this case. Instead of changing the order you can add the addtional paramter referencedColumnName to @JoinColumn.

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.