MappedBy reference an unknown target entity - java

I design a system of tables in the database for the film service. So far I have designed them in this way.
#Entity
#Table(name = "movies")
#Data
public class MovieEntity {
#Id
#Column(unique = true, updatable = false)
#GeneratedValue
private Long id;
#OneToMany(mappedBy = "movie", cascade = CascadeType.ALL)
private Set<MovieDescription> description;
}
#Entity
#Table(name = "movies_info")
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name = "type")
public abstract class MovieInfo {
#Id
#Column(unique = true, updatable = false)
#GeneratedValue
private Long id;
#ManyToOne
public MovieEntity movie;
}
#Entity
#DiscriminatorValue(value = EditType.Values.DESCRIPTION)
public class MovieDescription extends MovieInfo {
private String description;
private String language;
}
When compiling, it sends me a mistake
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.core.jpa.entity.MovieDescription.movie in com.core.jpa.entity.MovieEntity.description
Something related to MovieEnity mapping, but I don't know what it is all about.

use targetEntity attribute to target the super class field that you want to map with.
#Entity
#Table(name = "movies")
#Data
public class MovieEntity {
#Id
#Column(unique = true, updatable = false)
#GeneratedValue
private Long id;
#OneToMany(mappedBy = "movie", cascade = CascadeType.ALL, targetEntity= MovieInfo.class)
private Set<MovieDescription> description;
}
More detail: one to many mapping to a property of superclass

Related

Problems with identifiing entity in another entity using Hibernate

Here is the code:
#Entity
#Table(name = "students")
public class BotUser {
...
#Id
#Column(name = "id", updatable = false)
private int id;
#OneToOne
private Equipment equipment;
...
}
#Entity
#Table(name = "students")
public class Equipment {
...
#Id
#Column(name = "id")
private int id;
...
}
When Hibernate is fetching equipment data from PostrgeSQL table, he requests "equipment_id" field, not "id". How to solve this problem?
I must use #JoinColumn annotation. And to be sure I added CascadeType.ALL
#Entity
#Table(name = "students")
public class BotUser {
...
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "id")
private Equipment equipment;
...
}
The same problem: AnnotationException Referenced property not a (One|Many)ToOne

Hibernate - One to one relation single table inheritance

I have 3 entities BaseFoo, FooAbc, FooAbcDetail. FooAbc extends base entity BaseFoo. I'm trying to do one to one relation between FooAbc and FooAbcDetail.
#Data
#EqualsAndHashCode(callSuper = true)
#Entity
#Table(name = "foo")
#Audited
#AuditOverride(forClass = Auditable.class)
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name = "type_id", discriminatorType = DiscriminatorType.INTEGER)
public abstract class BaseFoo extends Auditable implements Serializable {
#Id
#Column(name = "id")
private Long id;
//other fields
}
#Data
#EqualsAndHashCode(callSuper = true)
#Entity
#Audited
#AuditOverride(forClass = BaseFoo.class)
#DiscriminatorValue("2")
public class FooAbc extends BaseFoo implements Serializable {
#EqualsAndHashCode.Exclude
#ToString.Exclude
#NotAudited
#OneToOne(mappedBy = "fooAbc",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true,
optional = false)
private FooAbcDetail fooAbcDetail;
//other fields
}
#Data
#Entity
#Table(name = "foo_abc_detail")
public class FooAbcDetail implements Serializable {
#Id
#Column(name = "foo_id"/* foo_abc_id (I tried both) */)
private Long id;
#ToString.Exclude
#EqualsAndHashCode.Exclude
#MapsId //also tried #MapsId("id")
#OneToOne(fetch = FetchType.LAZY)
private FooAbc fooAbc; //also tried BaseFoo
//other fields
}
While project starting up Hibernate throws:
org.hibernate.MappingException: Unable to find column with logical
name: id in org.hibernate.mapping.Table(public.foo_abc_detail) and its
related supertables and secondary tables
What is the problem here?
Environment
Hibernate 5.3.10.Final
Spring Boot 2.1.7.RELEASE
This error is telling you that there is no column on the foo_abc_detail table called foo_id.The column on foo_abc_detail is just called id.
#Data
#EqualsAndHashCode(callSuper = true)
#Entity
#Table(name = "foo")
#Audited
#AuditOverride(forClass = Auditable.class)
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name = "type_id", discriminatorType = DiscriminatorType.INTEGER)
public abstract class BaseFoo extends Auditable implements Serializable {
#Id
#Column(name = "id")
private Long id;
//other fields
}
#Data
#EqualsAndHashCode(callSuper = true)
#Entity
#Audited
#AuditOverride(forClass = BaseFoo.class)
#DiscriminatorValue("2")
public class FooAbc extends BaseFoo implements Serializable {
#EqualsAndHashCode.Exclude
#ToString.Exclude
#NotAudited
#OneToOne(mappedBy = "fooAbc",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true,
optional = false)
private FooAbcDetail fooAbcDetail;
//other fields
}
#Data
#Entity
#Table(name = "foo_abc_detail")
public class FooAbcDetail implements Serializable {
#Id
#Column(name = "id")
private Long id;
#ToString.Exclude
#EqualsAndHashCode.Exclude
#MapsId //also tried #MapsId("id")
#OneToOne(fetch = FetchType.LAZY)
private FooAbc fooAbc; //also tried BaseFoo
//other fields
}
Okey I solved. I don't know if this is the best way to do it but it works! I replaced the #MapsId annotation via #JoinColumn. Final result of the FooAbcDetail class like as below.
#Data
#Entity
#Table(name = "foo_abc_detail")
public class FooAbcDetail implements Serializable {
#Id
#Column(name = "id")
private Long id;
#ToString.Exclude
#EqualsAndHashCode.Exclude
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "id", referencedColumnName = "id", nullable = false)
private FooAbc fooAbc;
}

Hibernate mapping many columns to one table

How to configure mapping? I want to table "Remittance" were two columns referring to the table "Expense"
Remittance
#Entity
#Table(name = "REMITTANCE")
public class Remittance implements Serializable
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "REMITTANCE_ID")
private Long id;
#OneToMany(fetch = FetchType.EAGER, mappedBy = "remittances", cascade = CascadeType.ALL)
private Expense from;
#OneToMany(fetch = FetchType.EAGER, mappedBy = "remittances", cascade = CascadeType.ALL)
private Expense to;
}
Expense
#Entity
#Table(name = "EXPENSE")
public class Expense implements Serializable
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "EXPENSE_ID")
private Long id;
#ManyToOne(optional = false)
#JoinColumn(name = "REMITTANCE_ID")
private Remittance remittances;
}
I made a mistake. I have corrected
Remittance
#Entity
#Table(name = "REMITTANCE")
public class Remittance implements Serializable
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "REMITTANCE_ID")
private Long id;
#ManyToOne(optional = false)
#JoinColumn(name = "EXPENSE_ID")
private Expense from;
#ManyToOne(optional = false)
#JoinColumn(name = "EXPENSE_ID")
private Expense to;
}
Expense
#Entity
#Table(name = "EXPENSE")
public class Expense implements Serializable
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "EXPENSE_ID")
private Long id;
}
but now out
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: ru.make.alex.web.model.revenue.Remittance column: EXPENSE_ID (should be mapped with insert="false" update="false")

JPA, inheritance JOINED, multiple ID's

I have a little JPA question.
Assume you have such tables: (structure is fixed)
PERSON
--
ID,
DEPARTMENT_ID
...
SPECIALWORKER
------
ID, PERSON_ID, SPECIALDEPARTMENT_ID
...
DEPARTMENT
-------
ID,
...
SPECIALDEPARTMENT
-------
ID,
...
In Java I would build it with a simple hierarchy: SpecialWorker extends Person and SpecialDepartment extends Department. We will have "simple" Persons as well as "simple" Departments.
In JPA I try to build that scenario with a JOINED_Table inheritance, but I can't get it working. Any ideas?
edit
the coding, i hope i missed nothing.
I got a integrity exception when i try to insert a specialworker.
When i insert a specialworker jpa has to set the fk to departement (baseclass person) as well as the fk to the special departement (from the concrete current class)
#Entity
#Table(name = "DEPARTEMENT")
#Access(AccessType.FIELD)
#Inheritance(strategy = InheritanceType.JOINED)
public class Departement
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID")
private Integer id;
...
}
#Entity
#Table(name = "SPECIALDEPARTEMENT")
#Access(AccessType.FIELD)
#PrimaryKeyJoinColumn(name = "ID_DEPARTEMENT", referencedColumnName = "ID")
public class SpecialDepartement
extends Departement
implements Serializable
{
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID", updatable = false, insertable = false)
private Integer id01;
#OneToMany(mappedBy = "specialDepartement", cascade = CascadeType.ALL, orphanRemoval = true)
private List<SpecialWorker> workers;
...
}
#Entity
#Table(name = "PERSON")
#Access(AccessType.FIELD)
#Inheritance(strategy = InheritanceType.JOINED)
public class Person
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID")
#Max(19)
private Long id;
#ManyToOne
#JoinColumn(name = "ID_DEPARTEMENT", referencedColumnName = "ID", nullable = false)
private Departement departement;
...
}
#Entity
#Table(name = "SWORKER")
#Access(AccessType.FIELD)
#PrimaryKeyJoinColumn(name = "ID_PERSON", referencedColumnName = "ID")
public class SpecialWorker
extends Person
{
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID", updatable = false, insertable = false)
private Integer id01;
#ManyToOne
#JoinColumn(name = "ID_SPECIALDEPARTEMENT", referencedColumnName = "ID", nullable = false)
private SpecialDepartement specialdepartement;

JPA 2 - Foreign key that only includes one field from a composite primary key?

I'm having trouble getting a composite primary key and foreign keys working in JPA 2/Hibernate. I'm trying to create a simple scenario with countries and provinces:
Country Entity:
#Entity
#Table(name = "country")
public class Country extends DomainObjectBase implements Serializable {
#Id
#Basic(optional = false)
#Column(name = "code")
private String code;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "country")
private List<Province> provinces;
}
Province Primary Key:
#Embeddable
public class ProvincePK implements Serializable {
#Basic(optional = false)
#Column(name = "code")
private String code;
#Basic(optional = false)
#Column(name = "country_code")
private String countryCode;
}
Province Entity:
#Entity
#Table(name = "province")
public class Province extends DomainObjectBase implements Serializable {
#EmbeddedId
protected ProvincePK provincePK;
#MapsId("country_code")
#JoinColumn(name = "country_code", referencedColumnName = "code", insertable = false, updatable = false)
#ManyToOne(optional = false)
private Country country;
}
This create the correct tables for me with one exception:
Country Table:
code PK
...
Province Table
code PK FK - This is where the problem is its creating a foreign key reference to the country table's code column
country_code FK This is the only foreign key reference I want
...
How do I map my entities/composite key for hibernate to generate the schema I want? Right now I can't insert any data into province because its expecting that country contains the province code!
Thanks for any help.
Try this. I've found that it works for me when I work with data models like this.
#Entity
#Table(name = "province")
#IdClass(ProvincePK.class)
public class Province extends DomainObjectBase implements Serializable {
#Id
#Basic(optional = false)
#Column(name = "code")
private String code;
#Id
#Basic(optional = false, insertable = false, updatable = false)
#Column(name = "country_code")
private String countryCode;
#JoinColumn(name = "country_code", referencedColumnName = "code")
#ManyToOne
private Country country;
}
#MapsID argument must match to the name of attribute in ProvincePK class. #JoinColumn should be marked insertable=true,updatable=true, then it works. Here is the code -
#Entity
#Table(name = "province")
public class Province implements Serializable {
#EmbeddedId
protected ProvincePK provincePK;
#MapsId(value = "country_code")
#JoinColumn(name = "country_code", referencedColumnName = "code")
#ManyToOne(optional = false)
private Country country;
}
#Embeddable
public class ProvincePK implements Serializable {
#Basic(optional = false)
#Column(name = "code")
private String code;
#Basic(optional = false)
#Column(name = "country_code")
private String country_code;
}
#Entity
#Table(name = "country")
public class Country implements Serializable {
#Id
#Basic(optional = false)
#Column(name = "code")
private String code;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "country")
private List<Province> provinces;
}
Hope it helps.

Categories