-->
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.  [ 7 posts ] 
Author Message
 Post subject: how to map interfaces/abstract classes with annotations?
PostPosted: Tue Jul 07, 2009 11:46 am 
Newbie

Joined: Tue Jul 07, 2009 11:04 am
Posts: 4
hi!

i'm fighting since 2 days with this problem and i simply don't know how to solve it.

i have 2 classes, lets say Balance and ThermalConverter f.ex. which both implement the interface ThermalComponent (or extend the abstract class ThermalComponent .. both fine). This interface is empty because we just use it as a "flag". Balance and ThermalConverter are both mapped with @Entity (both in seperate tables) and they work just fine (fetching the data etcetc).

now i have a third class which contains a property of type ThermalComponent. how can i map this property with
hibernate?

interface ThermalComponent {} //empty

class Balance implements ThermalComponent {}

class ThermalConverter implements ThermalComponent {}

class ThermalStorage {
private ThermalComponent component;
}

my first guess was to define in ThermalComponent the annotations @MappedSuperclass and @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) and then just reference the with @JoinColumns like i do for all my other sub-entities. this won't work because hibernate complains that ThermalComponent is not of type entity. So i defined @Entity and now it complains that it can't find the the table ThermalComponent (which does not exist). i found out when using mapping files you could specify abstract=true and according to the hibernate documentation (chapter 9.1.5. Table per concrete class) it should not use a table for the abstract type then. how can i achieve the same with annotations?

my second guess was to use the @Any annotation like in this example http://www.jroller.com/eyallupu/entry/h ... annotation . problem with this approach is that i don't have a discriminator value.
basically for getting the value from the db the logic should be a sql-union which can return only 1 or 0 results.
i.e.
select tc from Balance union select tc from ThermalConverter where @JoinColumns;

is there a hibernate functionality which can do that? or do i have to implement it myself?

thx, sascha


Top
 Profile  
 
 Post subject: Re: how to map interfaces/abstract classes with annotations?
PostPosted: Tue Jul 07, 2009 12:33 pm 
Newbie

Joined: Tue Jul 07, 2009 11:04 am
Posts: 4
hmm .. after further reading I'd definetly would go with the abstract class not the interface as it seams that annotated interfaces are not supported anyway.

so in my case ThermalComponent would be a abstract class implementing the necessary @Id methods.


Top
 Profile  
 
 Post subject: Re: how to map interfaces/abstract classes with annotations?
PostPosted: Wed Jul 08, 2009 4:30 am 
Newbie

Joined: Tue Jul 07, 2009 11:04 am
Posts: 4
i'm still giving it a try .. now trying to use the @Any annotation again. looks like this ..

@Any(metaColumn = @Column(name = "object_type"))
@AnyMetaDef(idType = "ComponentId", metaType = "string", metaValues = {
@MetaValue(targetEntity = Balance.class, value = "BI"),
@MetaValue(targetEntity = ThermalConverter.class, value = "TE") })
@JoinColumns( { @JoinColumn(name = "TE_TTUNIT"),
@JoinColumn(name = "te_ttunit_konf_nr") })
private TemperatureComponent temperatureComponent;

i'm not sure what to use as idType in the @AnyMetaDef because we have our own class
which is just annotated with @Id. hibernate always throws the exception

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [spring-dao.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [spring-dao.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: property mapping has wrong number of columns: at.irm.iopt.model.domain.opt.components.ThermalConverter.temperatureComponent type: object
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [spring-dao.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: property mapping has wrong number of columns: at.irm.iopt.model.domain.opt.components.ThermalConverter.temperatureComponent type: object
Caused by: org.hibernate.MappingException: property mapping has wrong number of columns: at.irm.iopt.model.domain.opt.components.ThermalConverter.temperatureComponent type: object

i found this http://opensource.atlassian.com/project ... -patch.txt where he can use columns instead of column. would that be a solution for me?

@any ideas?


Top
 Profile  
 
 Post subject: Re: how to map interfaces/abstract classes with annotations?
PostPosted: Wed Sep 09, 2009 2:30 am 
Newbie

Joined: Tue Sep 08, 2009 10:50 am
Posts: 1
Hi bitkid,

I had the same problem as you. The solution was to use the @Any Annotation as you tried.

On http://stackoverflow.com/questions/217831/how-to-use-hibernate-any-related-annotations you see how to use it.

For your case it would be:

@Any(metaColumn = @Column(name = "te_ttunit_type"))
@AnyMetaDef(idType = "long", metaType = "string", metaValues = {
@MetaValue(targetEntity = Balance.class, value = "BI"),
@MetaValue(targetEntity = ThermalConverter.class, value = "TE") })
@JoinColumn(name = "te_ttunit_id")
private TemperatureComponent temperatureComponent;

If the id of you classes implementing temperatureComponent is Long.

Hope this still helps you.

Greetings


Top
 Profile  
 
 Post subject: Re: how to map interfaces/abstract classes with annotations?
PostPosted: Wed Nov 11, 2009 7:24 pm 
Newbie

Joined: Fri Nov 11, 2005 4:42 am
Posts: 17
Hi guys - I am using MySQL 5.1.33 and have set up the @any annotations like this

Code:
   @Any (metaColumn = @Column( name = "parent_type" ) )
    @AnyMetaDef( 
      idType = "long",
      metaType = "string",
       metaValues = {
           @MetaValue( value = "Client", targetEntity = Client.class ),
           @MetaValue( value = "Group", targetEntity = Group.class )
       }
    )
    @JoinColumn( name = "parent_id" )
   public com.xxx.model.Entity getParent() {
      return parent;
   }


maven-hibernate-plugin:hbm2ddl creates one field in the db and it is a tinyblob named 'parent'. Needless to say everything else doesn't work either!!

Could you help me?

ANSWER: MAKE SURE YOU HAVE VERSION 2.2 FOR THE PLUGIN or higher!!!!


Top
 Profile  
 
 Post subject: Re: how to map interfaces/abstract classes with annotations?
PostPosted: Thu Nov 12, 2009 7:21 pm 
Newbie

Joined: Fri Nov 11, 2005 4:42 am
Posts: 17
So now how do I map the other side of this relationship?
Code:
    @OneToMany
    @JoinColumn(name="parent_id", nullable=false)
   public Set<Group> getGroups() {
      return groups;
   }


results in:
Quote:
nested exception is org.hibernate.MappingException: Duplicate property mapping of _groupsBackref found in com.pascalmetrics.model.group.Group


and

Code:
    @OneToMany
   public Set<Group> getGroups() {
      return groups;
   }

results in:
an explosion of join tables being created and all mapped to the child's primary key.

and

Code:
   @OneToMany(mappedBy="parent")
   public Set<Group> getGroups() {
      return groups;
   }


results in:
Quote:
[INFO] Trace
org.hibernate.MappingException: Foreign key (FKA62B616067C4D314:_GROUP [parent_type,parent_id])) must have same number of columns as the referenced primary key (_GROUP [id])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:113)


Any working examples or thoughts would be greatly appreciated...any sort or resposne to let me know there is somebody out there would be a start.


Top
 Profile  
 
 Post subject: Re: how to map interfaces/abstract classes with annotations?
PostPosted: Mon Feb 22, 2010 8:20 am 
Newbie

Joined: Mon Feb 22, 2010 6:47 am
Posts: 5
Hi Monzi,
In the @any mapping below I am not clear what values need to be here , they are marked as red and whar they signify..?

@Any(metaColumn = @Column(name = "te_ttunit_type"))
@AnyMetaDef(idType = "long", metaType = "string", metaValues = {
@MetaValue(targetEntity = Balance.class, value = "BI"),
@MetaValue(targetEntity = ThermalConverter.class, value = "TE") })
@JoinColumn(name = "te_ttunit_id")
private TemperatureComponent temperatureComponent;


what is te_ttunit_type fields datatype?
and what does string signify in the metatype?
---------------------
also I am not having a common interface like TemperatureComponent so can Object be used here

my tables are as follows-
comment[id, name, data, parent_id]
document[id, name, data, size, parent_id]

here I need to get all the attachments to the parent id at once
I have created a table:

attachment table[id, attachment_id, attachment_type ]

here on auto generation of the hibernate pojo I have removed the attachment_id , attachment_type property mappings from the hbm files and the hibernate pojo
and need to apply the @any

@Any(metaColumn = @Column(name = "attachment_type"))
@AnyMetaDef(idType = "long", metaType = "string", metaValues = {
@MetaValue(targetEntity = Comment.class, value = "com"),
@MetaValue(targetEntity = Document.class, value = "doc") })
@JoinColumn(name = "attachment_id")
private Object attachment;

Is this right?
as the attachment object is null when the listing is made..
where am I wrong..?
please let me know..

thanks,
Komal


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