Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@MappedSuperclass and @OneToMany

I need association @OneToMany from Country to superclass Place (@MappedSuperclass). It could be bidirectional. I would need something like @OneToAny.

@MappedSuperclass
public class Place {

    private String name;
    private Country country;

    @Column
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne
    @JoinColumn(name="country_id")
    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }
}

Country:

@Entity
   public class Country {
   private long id;
   private String name;
   private List<Place> places;

   @Any(metaColumn = @Column(name = "place_type"), fetch = FetchType.EAGER)
   @AnyMetaDef(idType = "integer", metaType = "string", metaValues = {
         @MetaValue(value = "C", targetEntity = City.class),
         @MetaValue(value = "R", targetEntity = Region.class) })
   @Cascade({ org.hibernate.annotations.CascadeType.ALL })
   //@JoinColumn(name="unnecessary") 
   //@OneToMany(mappedBy="country")  // if this, NullPointerException...
   public List<Place> getPlaces() {
      return places;
   }
//and rest of class

Without @JoinColunm there is an exception:

Caused by: org.hibernate.AnnotationException: @Any requires an explicit @JoinColumn(s): tour.spring.bc.model.vo.Country.places

In table City and Region is foreign key to table Country (Region.country_id, City.country_id) which is ok. But I do NOT need foreign key in table Country to tables Region and City so I don't need @JoinColum.

What can I do?

like image 429
LancerX Avatar asked Jan 22 '11 18:01

LancerX


People also ask

When to use @OneToMany or @manytomany for unmapped classes?

Use of @OneToMany or @ManyToMany targeting an unmapped class: com.x.y.model.Role.usersThis is because you are using mapping in a @MapppedSuperClass. Such classes are not mapped in to a table by the Java persistence provider.

What is the mapped superclass strategy?

The mapped superclass strategy is the simplest approach to mapping an inheritance structure to database tables. It maps each concrete class to its own table. That allows you to share the attribute definition between multiple entities.

What is the difference between table per class and mapped superclass?

The table per class strategy is similar to the mapped superclass strategy. The main difference is that the superclass is now also an entity. Each of the concrete classes gets still mapped to its own database table. This mapping allows you to use polymorphic queries and to define relationships to the superclass.

What is a mapped superclass in Salesforce?

A mapped superclass is not an entity, and there is no table for it. That means that you can’t use polymorphic queries that select all Publication entities and you also can’t define a relationship between an Author entity and all Publication s.


1 Answers

@Any doesn't make sense here since foreign key is at the Places side and therefore it doesn't need additional meta column.

I'm not sure if it's possible to create a polymorphic relationship to @MappedSuperclass. However, you can try to declare Place as @Entity @Inheritance(InheritanceType.TABLE_PER_CLASS), it should produce the same database schema and allow polymorpic relationship.

like image 101
axtavt Avatar answered Oct 07 '22 00:10

axtavt