-->
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: Question regarding the @any implementation
PostPosted: Tue Feb 23, 2010 2:53 am 
Newbie

Joined: Mon Feb 22, 2010 6:47 am
Posts: 5
Hi,

I am trying to get a heterogeneous list of objects in a list through hibernate and am using the @any mapping.
I have done the following it compiles and executes but it does not return any object
here is the pojo
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Any;
import org.hibernate.annotations.AnyMetaDef;
import org.hibernate.annotations.MetaValue;

@Entity
@Table(name = "entity_instance")
public class EntityInstance implements java.io.Serializable {

private Object attachment;
private Long id;
public EntityInstance() {
}
@Any(metaColumn = @Column(name = "entityType"))
@AnyMetaDef(idType = "long", metaType = "string", metaValues = {
@MetaValue(targetEntity = Comment.class, value = "C"),
@MetaValue(targetEntity = Document.class, value = "D") })
@JoinColumn(name = "instanceId")

public Object getAttachment() {
return attachment;
}

public void setAttachment(Object attachment) {
this. attachment = attachment;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}
}

and my hbm.xml file is as follows:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 22 Nov, 2009 11:31:40 AM by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
<class name="com.ensarm.enwelib.orm.hibernate.domain.EntityInstance" table="entity_instance" >
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
</class>
</hibernate-mapping>

here is the listing..
the record is as follows on the db
entity_instance
[1957, 1, C]


Session session = null;
try {
session = SchemaManager.getOrmSession();
Query query = session.createQuery("from EntityInstance where id=:value");
query.setLong("value", 1957);
List entitylist = query.list();

SchemaManager.releaseSession(session);
}
catch (Exception e) {
e.printStackTrace();
}


please let me know here I am wrong..


Top
 Profile  
 
 Post subject: Re: Question regarding the @any implementation
PostPosted: Thu Feb 25, 2010 2:55 am 
Newbie

Joined: Mon Feb 22, 2010 6:47 am
Posts: 5
Hi,

The above implementation is right.
problem was actually
I was using the Configuration instead of AnnotationConfiguration to build the factory and hence it was not respecting the annotation
a few changes in the .cfg.xml file the above code works absolutely fine.I was also able to get the heterogeneous list of objects


but now when I am trying to save a new borrow entity with a new any object it does not work.
as shown in the example patch at this like from ur site..
http://opensource.atlassian.com/project ... -patch.txt

+ PropertySet set2 = new PropertySet("integer");
+ property = new IntegerProperty("age", 33);
+ set2.setSomeProperty(property);

+ set2.addGeneratedProperty(property);
+ s.save(set2);

+
+ t.commit();
+ s.close();


where I am also trying the same thing.

Book bk = new Book();
bk.setName("java");
Borrow brw = new Borrow();
brw.setItem(bk);
t.begin();
session.save(brw);


t.commit();

but it throws this exception

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.ensarm.enwelib.orm.hibernate.domain.Book


at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:242)
at org.hibernate.type.AnyType.nullSafeSet(AnyType.java:154)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2025)


at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2399)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2335)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2635)


at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:115)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)


at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)


at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)


at com.ensarm.enwelib.orm.hibernate.AnyTest.testAny(AnyTest.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)


at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)


at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)


at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
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)

please let me know where I am going wrong...

Komal


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.