AnnotationException: MappedBy reference an unknown target entity property - mapping an inherited class - java

I have such entities with inheritance
#MappedSuperclass
public class PostEntity {
...
#ManyToOne
#JoinColumn(name = "user_id")
private UserEntity author;
}
#Entity
#Table(name = "answers")
public class AnswerEntity extends PostEntity {}
#Entity
#Table(name = "users")
public class UserEntity {
...
#OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private List<AnswerEntity> answers;
}
during compilation, he throws me away
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.example.demo.jpa.entity.AnswerEntity.author in com.example.demo.jpa.entity.UserEntity.answers
I do not know why he does not see the author field during mapping.

It is not supported by JPA, at least not by Hibernate. You can either have:
#OneToMany(mappedBy = "author")
private List<PostEntity> answers;
Or move UserEntity author field to AnswerEntity.

Related

How to map relationship with multiple target entities

I don't understand how to map bVersionId.
#Data
#Table(name = "prefix_class_a")
ClassA{
//...
private Long bVersionId;
//...
}
This tables already have ManyToOne relationship.
#Data
#Table(name = "prefix_class_b")
ClassB{
//...
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY, mappedBy = "classB")
private Collection<ClassBVersion> versions = new ArrayList<>();
//...
}
#Data
#Table(name = "prefix_class_b_version")
ClassBVersion{
//...
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "prefix_class_b_id")
private ClassB classB;
//...
}
what relation should be used? And how?

Hibernate find method sometimes returns proxy

Lately my project encountered a really odd issue with JPA findOne(id) returning a proxy object instead of a full object.
Here is the scenario. Consider the entities and their connections shown below.
#Table(name = "HOUSE")
#Entity
#EqualsAndHashCode
#Setter
#ReadPermission(expression = "user has rights for template snapshots AND has filter")
public class HouseEntity extends VersionedEntity {
#OneToMany(cascade = CascadeType.ALL, mappedBy = "house", fetch = FetchType.LAZY, orphanRemoval = true)
public List<RoomEntity> getRooms() {
return rooms;
}
#OneToMany(cascade = CascadeType.ALL, mappedBy = "template", fetch = FetchType.LAZY, orphanRemoval = true)
public List<TableEntity> getTables() {
return tables;
}
}
#Entity
#Table(name = "ROOMS")
public class Room {
#ManyToOne(fetch = FetchType.LAZY)
public HouseEntity getHouse() {
return house;
}
#OneToMany(mappedBy = "room", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
public List<TableEntity> getTables() {
return tables;
}
}
#Entity
#Table(name = "TABLES")
public class TableEntity{
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "HOUSE_ID")
public HouseEntity getHouse() {
return template;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "ROOM_ID")
public RoomEntity geRoom() {
return room;
}
As you can see, House has Tables and Rooms, Rooms also has Tables, and each child entity has a connection to its parents.
Add a table to the HouseEntity
Remove the table from the HouseEntity immediately after.
For 1, the houseRepository.findById gets my HouseEntity wrapped in proxy, as if it is lazy loaded.
The weird part is that if I do:
Add a table to a RoomEntity, which is a child of HouseEntity.
Remove the table from RoomEntity.
Then houseRepository.findById returns the HouseEntity without the proxy.
My question here is, why would this happen? Why would the findById method return a proxyed entity in this case? I need to have access to the normal entity without the proxy directly, even if the proxy has the entity populated in the target.

Ebean ManyToMany within Join Table?

I'm using ebean with play framework and I have a problem mapping a many-many relationship between 2 join tables. Let's say we have 3 entities, Person, Company, and Profession. A Person can have many Professions. A Person can work for many Companies. And a Company can employ many Persons in many Professions.
Here's my code:
#Entity
public class Company extends Model {
#Id
public Long id;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "company")
public List<CompanyPerson> companyPersons;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "company")
public List<CompanyProfession> companyProfessions;
...
}
#Entity
public class Profession extends Model {
#Id
public Long id;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "profession")
public List<CompanyProfession> companyProfessions;
...
}
#Entity
public class Person extends Model {
#Id
public Long id;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "person")
public List<CompanyPerson> companyPersons;
...
}
#Entity
public class CompanyPerson extends Model {
#EmbeddedId
public CompanyPersonId id = new CompanyPersonId();
#ManyToOne
#JoinColumn(name = "company_id")
private Company company;
#ManyToOne
#JoinColumn(name = "person_id")
private Person person;
#ManyToMany(mappedBy = "companyPersons")
public List<CompanyProfession> companyProfessions;
...
}
#Entity
public class CompanyProfession extends Model {
#EmbeddedId
public CompanyProfessionId id = new CompanyProfessionId();
#ManyToOne
#JoinColumn(name = "company_id")
private Company company;
#ManyToOne
#JoinColumn(name = "profession_id")
private Person person;
#ManyToMany(cascade = CascadeType.ALL)
public List<CompanyPerson> companyPersons;
...
}
I get the following error:
Error injecting constructor, javax.persistence.PersistenceException: Error with the Join on [models.CompanyProfession.companyPersons]. Could not find the local match for [null] Perhaps an error in a #JoinColumn
at play.db.ebean.EbeanDynamicEvolutions.(EbeanDynamicEvolutions.java:36)
I tried adding JoinColumn(name = "companyperson_id") but that just changed the error from [null] to [companyperson_id].
Not sure what to do here. Any help?

hibernate. OneToOne mapping in database

I have following mapping:
#Entity
#Table(name="terminal_user")
public class TerminalUser {
#OneToMany(mappedBy="user",fetch = FetchType.LAZY)
private Set<UserContent> userContentSet;
....
}
#Entity
#Table(name="user_content")
public class UserContent {
#ManyToOne
#JoinColumn(name = "user_id")
TerminalUser user;
#OneToOne(mappedBy = "userContent")
Content content;
...
}
#Entity
#Table(name = "content")
public class Content {
#OneToOne
#JoinColumn(name = "user_content_id")
UserContent userContent;
....
}
After setting hibernate.hbm2ddl.auto to create
I see following database diagram:
For me relation of tables user_content and content is not one to one.
It looks same as relation of terminal_user and user_content and it is one to many
What do I wrong?
A unique constraint will make the difference:
#Entity
#Table(name = "content")
public class Content {
#OneToOne
#JoinColumn(name = "user_content_id", unique=true)
UserContent userContent;
....
}

JPA/ Hibernate query child property

I use the single table strategy (with discriminator) to use inheritance
UML schema => http://yuml.me/67acf6a6
I would like to fetch all the orders of a customer with all the associations related (cars, books and tvs). Do you know how to achieve this without breaking the model classes.
#Entity
#Table(name = "customers")
public class Customer{
private Date birthDate;
private String name;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true)
#OrderBy("order")
private List<? extends Order> orders;
}
#Entity
#Table(name = "orders")
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class Order{
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#NotNull
#JoinColumn(name = "CustomerID")
private Customer customer;
}
#Entity
#DiscriminatorValue("book")
public class BookOrder extends Order {
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name="book_id")
private Set<Book> books;
}
#Entity
#DiscriminatorValue("car")
public class CarOrder extends Order {
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name="car_id")
private Set<Car> cars;
}
#Entity
#DiscriminatorValue("tv")
public class TvOrder extends Order {
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name="tv_id")
private Set<Tv> tvs;
}
If I'm doing in HQL
select cutomer from customer customer
inner join fetch customer.orders order
left join fetch order.cars
left join fetch order.books
left join fetch order.tvs
I'm getting the error
org.hibernate.QueryException: could not resolve property: cars,
It makes sense in the abstract class Order this field doesnt exist.
Do you know how i can achieve this ? what is the recommendation of hibernate in this case ?
My goal is to simple a simple query and to fetch everything.
If you do the following query
select c from customer c
join fetch c.orders
then you retrieve all the customers with any types of the orders (cars | books | tvs). Those orders are polymorphic, if do instanceof for each Customer.orders element you can determine what type of order element is.

Categories