-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate Search Cast Problem
PostPosted: Mon Sep 27, 2010 6:36 am 
Newbie

Joined: Fri Sep 03, 2010 4:14 am
Posts: 3
Hi,
We have two tables Assay and Project and one Project has many assays.
The schema is given below.

Schema for Assay_Test (Assay)
PRIMARY_KEY NUMBER(10,0)
COLLECTION_KEY NUMBER(10,0)
ASSAY_NAME VARCHAR2(4000 BYTE)
PROJECT_ID_FK VARCHAR2(4000 BYTE)



Schema for Project_Test (Project)
PRIMARY_KEY NUMBER(10,0)
COLLECTION_KEY NUMBER(10,0)
PROJECT_NAME VARCHAR2(4000 BYTE)

The relationship between Assay and Project is between 'PROJECT_ID_FK' of Assay and 'COLLECTION_KEY' of Project.Using Hibernate session with Annotations ,I assigned @DocumentID annotation to 'PRIMARY_KEY' of Assay and Project because it is unique.
After this configuration when i run the program and try to fetch the list of Assays using Project object, I get the following error.

Message:com.entities.ProjectTest cannot be cast to java.math.BigDecimal
java.lang.ClassCastException: com.entities.ProjectTest cannot be cast to java.math.BigDecimal
at org.hibernate.type.BigDecimalType.getHashCode(BigDecimalType.java:71)
at org.hibernate.type.AbstractType.getHashCode(AbstractType.java:144)
at org.hibernate.engine.EntityKey.generateHashCode(EntityKey.java:126)
at org.hibernate.engine.EntityKey.<init>(EntityKey.java:70)
at org.hibernate.engine.StatefulPersistenceContext.getCollectionOwner(StatefulPersistenceContext.java:733)
at org.hibernate.loader.Loader.readCollectionElement(Loader.java:1037)
at org.hibernate.loader.Loader.readCollectionElements(Loader.java:690)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:630)
at org.hibernate.loader.Loader.doQuery(Loader.java:745)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2062)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:628)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1853)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
at Main.search2(Main.java:133)
at Main.main(Main.java:48)

The error occurs at the following code snippet
Set<AssayTest> list=project.getAssayTests();
for (AssayTest assay:list){
System.out.println("assay name from project" +assay.getAssayName());


}

If i change the @DocumentID to 'COLLECTION_KEY' of Project, the error vanishes and program runs fine.However i can't make 'Collection_KEY' as @DocumentID because it is not unique for data in our production server.Only Primary_key is unique.
so is there any restriction in Hibernate Search that you have to annotate @DocumentID to an attribute that has been used in specifying relationship between the entities.Is there any workaround this limitation?.
Any help or comments would be appreciated.
Thanks,
Ali

Here is the Assay_Test Entity

/**
* The persistent class for the ASSAY_TEST database table.
*
*/
@Indexed
@Entity
@Table(name="ASSAY_TEST")
public class AssayTest implements Serializable {
private static final long serialVersionUID = 1L;
@Field
@Column(name="ASSAY_NAME", length=4000)
private String assayName;
@Field
@Column(name="COLLECTION_KEY", precision=10)
private BigDecimal collectionKey;
@Id
@DocumentId
@Field
@Column(name="PRIMARY_KEY", precision=10)
private BigDecimal primaryKey;

//bi-directional many-to-one association to ProjectTest
@NotFound(action=NotFoundAction.IGNORE)
@ManyToOne
@JoinColumn(name="PROJECT_ID_FK", referencedColumnName="COLLECTION_KEY")
private ProjectTest projectTest;

public AssayTest() {
}

public String getAssayName() {
return this.assayName;
}

public void setAssayName(String assayName) {
this.assayName = assayName;
}

public BigDecimal getCollectionKey() {
return this.collectionKey;
}

public void setCollectionKey(BigDecimal collectionKey) {
this.collectionKey = collectionKey;
}

public BigDecimal getPrimaryKey() {
return this.primaryKey;
}

public void setPrimaryKey(BigDecimal primaryKey) {
this.primaryKey = primaryKey;
}

public ProjectTest getProjectTest() {
return this.projectTest;
}

public void setProjectTest(ProjectTest projectTest) {
this.projectTest = projectTest;
}

}

Here is the Project_Test Entity

package com.entities;

/**
* The persistent class for the PROJECT_TEST database table.
*
*/
@Indexed
@Entity
@Table(name="PROJECT_TEST")
public class ProjectTest implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@DocumentId
@Field
@Column(name="PRIMARY_KEY", precision=10)
private BigDecimal primaryKey;
@Field
@Column(name="PROJECT_NAME", length=4000)
private String projectName;

@Field
@Column(name="COLLECTION_KEY")
private String collectionKey;
//bi-directional many-to-one association to AssayTest
@NotFound(action=NotFoundAction.IGNORE)
@OneToMany(mappedBy="projectTest")
private Set<AssayTest> assayTests;

public ProjectTest() {
}

public BigDecimal getPrimaryKey() {
return this.primaryKey;
}

public void setPrimaryKey(BigDecimal primaryKey) {
this.primaryKey = primaryKey;
}

public String getProjectName() {
return this.projectName;
}

public void setProjectName(String projectName) {
this.projectName = projectName;
}

public Set<AssayTest> getAssayTests() {
return this.assayTests;
}

public void setAssayTests(Set<AssayTest> assayTests) {
this.assayTests = assayTests;
}

public void setCollectionKey(String collectionKey) {
this.collectionKey = collectionKey;
}

public String getCollectionKey() {
return collectionKey;
}

}


Top
 Profile  
 
 Post subject: Re: Hibernate Search Cast Problem
PostPosted: Mon Sep 27, 2010 9:27 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
so is there any restriction in Hibernate Search that you have to annotate @DocumentID to an attribute that has been used in specifying relationship between the entities.

No, there is no such limitation.
In the class AssayTest you don't need the additional @Field annotation on top of @DocumentId and @Id. In fact you don't even need (depending on your Search version) the @DocumentId. It defaults to the entity id.

How does the code between opening and closing the session look like which causes the error?


Top
 Profile  
 
 Post subject: Re: Hibernate Search Cast Problem
PostPosted: Tue Sep 28, 2010 2:14 pm 
Newbie

Joined: Fri Sep 03, 2010 4:14 am
Posts: 3
Quote:
How does the code between opening and closing the session look like which causes the error?

Thank you for your reply hardy.

Here is the code between opening and closing of session which i had written for testing the problem.

FullTextSession fullTextSession = Search.getFullTextSession(session);
Class[] classes={AssayTest.class,ProjectTest.class};
String[] fields=null;
try {
fields=Indexing.reindexEntities(classes,fullTextSession); //reindex entities and return names of fields in array
} catch (Exception e) {
e.printStackTrace();
}

// No problem upto this
Transaction tx = fullTextSession.beginTransaction();
MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
org.apache.lucene.search.Query query=null;
try {
query = parser.parse("ProjectNestle");
// wrap Lucene query in a org.hibernate.Query
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, classes);
// execute search
List result = hibQuery.list();
System.out.println("Hibernate result size:"+result.size());
for (Object object:result){
Class c=object.getClass();
System.out.println("Name:"+c.getName());
if (object instanceof ProjectTest){
ProjectTest project=(ProjectTest)object;
System.out.println("Project name:"+project.getProjectName());

//Problematic code begins
Set<AssayTest> list=project.getAssayTests();
for (AssayTest assay:list){
System.out.println("assay name from project" +assay.getAssayName());

}
//Problematic code ends
}

}
}
catch (Exception e) {
System.out.println("Message:"+e.getMessage());
e.printStackTrace();
}
tx.commit();
session.close();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.