-->
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: @OneToOne pointing to a abstract @MappedSuperclass
PostPosted: Wed Mar 04, 2009 8:20 am 
Newbie

Joined: Wed Mar 04, 2009 7:56 am
Posts: 2
Hi,

Sorry if this is a FAQ, but I've googled for a few hours and didn't come by an answer... Anyway, I'd like to have a @OneToOne relationship to a abstract class (or a interface would work too) that is implemented by several actual entities. Something like this:
Code:
@MappedSuperclass
public abstract class AbstractShape {
   ...
}

@Entity
public class Rectangle extends AbstractShape {
   ...
}

@Entity
public class Circle extends AbstractShape {
   ...
}

@Entity
public class ShapeOwner {   
   private AbstractShape shape;
   
   @OneToOne
   public AbstractShape getShape() {
      return shape;
   }
   
   public AbstractShape setShape(AbstractShape shape) {
      this.shape = shape;
   }

       ...
}


This will of course result in "org.hibernate.AnnotationException: @OneToOne or @ManyToOne on example.ShapeOwner references an unknown entity: example.AbstractShape", which is correct as AbstractShape is not an @Entity.

So, how to solve the problem? I can't give a targetEntity for the @OneToOne annotation, since there is several entities I want it to target. I guess I could mark Shape as an @Entity, but this feels kludgy to me. Or is it just me? What's the correct way to do what I want?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 9:30 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
You're trying to map the association to the class that is not entity (AbstractShape), i.e. @MappedSuperclass presence doesn't imply that the marked class is entity. If you still want to use a 'table-per-concrete-class' strategy just switch to the 'Table per concrete class with unions' variation:

AbstractShape
Code:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractShape {

    private Long id;
    private String shapeField;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    public Long getId() {
        return id;
    }

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

    @Column
    public String getShapeField() {
        return shapeField;
    }

    public void setShapeField(String shapeField) {
        this.shapeField = shapeField;
    }
}


Rectangle
Code:
@Entity
public class Rectangle extends AbstractShape {

    private String rectangleField;

    @Column
    public String getRectangleField() {
        return rectangleField;
    }

    public void setRectangleField(String rectangleField) {
        this.rectangleField = rectangleField;
    }
}


Circle
Code:
@Entity
public class Circle extends AbstractShape {

    private String circleField;

    @Column
    public String getCircleField() {
        return circleField;
    }

    public void setCircleField(String circleField) {
        this.circleField = circleField;
    }
}


The most important point here is a key generation strategy for the AbstractShape class - the key must be unique for all three tables (for the AbstractShape, Rectangle and Circle). The best decision is to use sequence-based generation strategy if your database supports it. Given 'table' strategy is used just for the presentation purposes.


Last edited by denis.zhdanov on Wed Mar 04, 2009 9:39 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 9:39 am 
Newbie

Joined: Wed Mar 04, 2009 7:56 am
Posts: 2
Thanks, just what I need :)


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.