2

I want to achieve something like this:

@Entity
@Table(name = "beer")
public class Beer {

    @Id Long id;
    String brand;
    String name;
}

@Entity
public class BeerWithIngredients extends Beer {

    @OneToMany(mappedBy="beer")
    List<BeerIngredient> ingredients; 
}

@Entity
public class BeerIngredient {

    @Id Long id;
    // .. whatever fields

    @ManyToOne
    @JoinColumn(name = "beer_id")
    BeerWithIngredient beer;
}

Physically, all beer data is in one table, but I want to split it into more entities.

Please note that:

  • I would not like to use discriminator column in the database
  • There isn't a column that I could use for @DiscriminatorFormula
  • I do not want to embed Beer inside BeerWithIngredients, because it essentially is not a composition, but proper inheritance

Is there a way of achieving this with JPA (Hibernate)? Right now it complains about missing discriminator column, that I don't plan to provide.

Xorty
  • 18,367
  • 27
  • 104
  • 155
  • 1
    And how would you like the system to know which object to build if you refuse to provide an information by which they can be distinguished (i.e. the discriminator column). The system cannot just guess it, can it? If you have only 2 classes in your hierarchy you could use one of the "...whatever fields" in BeerIndgredient to be your discriminator column, but it will stop working once you have more subclasses. Is there any valid reason for not having extra column. – Zielu Feb 17 '15 at 17:27

1 Answers1

0

Introduce a new super class RootBeer with all common beer properties. Annotate this class with MappedSuperClass. Make Beer and BeerWithIngredients inherit from RootBeer and annotate them with Entity as well as Table(name="beer").

For an example check this.

Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60
  • That'd work as long as I don't need to use both Beer and BeerWithIngredients (imagine you are serving simplified Beer to a mobile client and richer BeerWithIngredients to a desktop client). Any workaround to keep using both entities? – Xorty Feb 17 '15 at 16:28
  • Aren't you trying to solve this at the wrong level then? See, for example, http://stackoverflow.com/questions/28447922/spring-one-jpa-model-many-json-respresentations @Xorty – Alan Hay Feb 17 '15 at 19:31
  • @Alan yeah that's quite likely - but I want to prevent unnecessary joins as well :/ – Xorty Feb 17 '15 at 19:33
  • Well if you don't want to write specific service calls per use case then use the Open Session in View pattern or try this new feature of Hibernate which I haven't used yet but appears to do much the same. http://stackoverflow.com/questions/25362831/solve-hibernate-lazy-init-issue-with-hibernate-enable-lazy-load-no-trans – Alan Hay Feb 17 '15 at 19:43
  • @Xorty right you are - I have modified my answer to make both Beer and BeerWithIngredients available to you. – Fritz Duchardt Feb 18 '15 at 08:34
  • @Fritz thanks again, but I am afraid that now we can't do it without discriminator column or discriminator formula on entities. – Xorty Feb 18 '15 at 08:49
  • RootBeer, the parent of all beers – Adriaan Koster Nov 06 '19 at 14:39