I have an entity:
#Entity
#Table(name = "person")
public class Person {
#EmbeddedId
private PersonId id;
#OnDelete(action = OnDeleteAction.CASCADE)
#MapsId("a_phoneNumberId")
#ManyToOne
private PhoneNumber phoneNumber;
#OnDelete(action = OnDeleteAction.CASCADE)
#MapsId("b_addressId")
#ManyToOne
private Address address;
...
with embedded id:
#Embeddable
public class PersonId implements Serializable {
private int a_phoneNumberId;
private int b_addressId;
...
Note: a_ and b_ prefixes are used to order columns in primary key.
Everything works as expected and hibernate generates a table with columns: phoneNumber_id and address_id.
Is it possible to rename those columns, as I want to have a snake_case name - phone_number_id?
So far I tried
#AttributeOverride annotation:
#Entity
#Table(name = "person")
public class Person {
#EmbeddedId
#AttributeOverrides({
#AttributeOverride(name = "a_phoneNumberId", column = #Column(name = "phone_number_id"))
})
private PersonId id;
#Column annotation for the id:
#Embeddable
public class PersonId implements Serializable {
#Column(name = "phone_number_id")
private int a_phoneNumberId;
but it changed nothing.
Related
Let's say I have those two entities, Person & Insurance. One Person can have multiple insurances, and the insurance uniqueness is maintained by a composite key combination of (insurance type, policy number, and person id). The below code represent the the scenario...
parent class
#Entity
#Setter
#Getter
#RequiredArgsConstructor
public class Person implements Serializable {
#Id
#GeneratedValue(strategy = "GenerationType.IDENTITY")
#Column(name "person_id")
private Long personId;
#Column(name = "fst_nm")
private String fstName;
#Column(name = "lst_nm")
private String lstNm;
// ..Other columns & relationships
#OneToMany(mappedBy = "person")
private List<Insurance> insurances;
public void addInsurance(Insurance toAdd) {
getInsurances().add(toAdd);
toAdd.setPerson(this);
}
}
child class
#Entity
#Setter
#Getter
#RequiredArgsConstructor
public class Insurance implements Serializable {
#EmbeddedId
private insurancePK id;
//other data
#ManyToOne
#MapsId("personId")
private Person person;
}
composite PK class
#Setter
#Getter
#Embeddable
public class InsurancePK implements Serializable {
#Column(name = "person_id", insertable = false, updatable = false)
private Long personId;
#Column(name = "insurance_type")
private String insuranceType;
#Column(name = "pol_num")
private String polNum;
}
now, my data mapper looks something like that...
Person newPerson = new Person();
newPerson.setInsurances(new ArrayList<>());
// fill out Person Model data
// incoming insurance data
while (incomingData.hasNext()) {
Insurance insuranceData = new Insurance();
InsurancePK pk = new InsurancePK();
// set other insurance data
pk.setInsuranceType("Dental");
pk.setPolNum("123Abc00");
insuranceData.setId(pk);
person.addInsurance(insuranceData);
}
Problem is my person_id inside the composite key is always getting a null value, not sure why (shouldn't the #MapsId takes care of that value)?
I need to fetch that value dynamically, most of the JPA composite key solutions only are setting all the value manually, but that's not my scenario.
return object from saveAndflush()
{
person: {
person_id: 55,
fst_nm: blah,
lst_nm: blah,
insurances: [
{
insurance_pk: {
person_id: null,
insurance_type: "Dental",
pol_num: "123Abc00"
}
//other insurance data
}
]
}
}
any suggestions on what am I missing? Thank you in advance!
Remove the #Column(name = "person_id", insertable = false, updatable = false) annotation from the InsurancePK.personId.
Add the following annotation:
#JoinColumn(name = "name = "person_id"")
to the Insurance.person.
As mentioned in the comments, adding a cascade to my entity column started me on the right track.
just in case, that's the model that worked for me after couple of tries
Parent class
#Entity
#Setter
#Getter
#RequiredArgsConstructor
public class Person implements Serializable {
#Id
#GeneratedValue(strategy = "GenerationType.IDENTITY")
#Column(name "person_id")
private Long personId;
#Column(name = "fst_nm")
private String fstName;
#Column(name = "lst_nm")
private String lstNm;
// ..Other columns & relationships
// cascade added <-- thanks to SternK
#OneToMany(mappedBy = "person", casecade = CascadeType.ALL, orphanRemoval = true)
private List<Insurance> insurances;
public void addInsurance(Insurance toAdd) {
getInsurances().add(toAdd);
toAdd.setPerson(this);
}
}
Child class
#Entity
#Setter
#Getter
#RequiredArgsConstructor
public class Insurance implements Serializable {
#EmbeddedId
private insurancePK id;
//other data
#ManyToOne
#MapsId("personId")
// annotation added here instead of PK class <-- fix
#JoinColumn(name="person_id", nullable = false, insertable = false, updatable = false)
private Person person;
}
PK class
#Setter
#Getter
#Embeddable
public class InsurancePK implements Serializable {
//annotation removed <-- fix thanks to SternK
private Long personId;
#Column(name = "insurance_type")
private String insuranceType;
#Column(name = "pol_num")
private String polNum;
}
Greetings to the community,
I am struggling all day to find a solution to the issue below.
The scenario is the following, I have a table
---TABLE_ONE---
INT ID
VARCHAR NAME
PRIMARY_KEY (ID)
and my other table consisted of three columns which consist together a composite key
---TABLE_TWO---
INT TABLE_ONE_ID (FK -> TABLE_ONE.ID)
VARCHAR NAME
VARCHAR EMAIL
PRIMARY_KEY(TABLE_ONE_ID, NAME, EMAIL)
The relationship I want to achieve is that the TABLE_ONE entity will
have a list of objects from the TABLE_TWO (one-to-many relationship).
I tried to do this with as shown below.
#Entity
#Table(name = "TABLE_ONE")
public class TableOne {
#Column(name="id")
private int id;
#Column(name="name")
private String name
#OneToMany(fetch = FetchType.EAGER, mappedBy = "tableOne")
private List<TableTwo> tableTwoList;
//getters, setters, constructors
}
#Entity
#Table(name = "TABLE_TWO")
public class TableTwo {
#EmbeddedId
private TableTwoCompositeId tableTwoCompositeId;
#ManyToOne
#JoinColumn(name = "TABLE_ONE_ID", referencedColumnName = "ID", insertable = false, updatable = false)
private TableOne tableOne;
//getters, setters, constructors
}
#Embeddable
public class TableTwoCompositeId {
#Column(name = "TABLE_ONE_ID")
public Integer provider;
#Column(name = "NAME")
public String name;
#Column(name = "EMAIL")
public String email;
//getters, setters, constructors
}
However, I'm getting javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet and Caused by: java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist when a TableOne object is retrieved from the database.
Thanks in advance for any help!
I think you need several minor changes:
TableOne.id needs an #Id annotation
The type of TableTwoCompositeId.provider should match the type of TableOne.id
TableTwo.tableOne needs a #MapsId annotation to indicate it maps TableTwoCompositeId.provider
Here is how the code should look:
#Entity
#Table(name = "TABLE_ONE")
public class TableOne {
#Id
#Column(name="id")
private int id;
#Column(name="name")
private String name
#OneToMany(fetch = FetchType.EAGER, mappedBy = "tableOne")
private List<TableTwo> tableTwoList;
//getters, setters, constructors
}
#Entity
#Table(name = "TABLE_TWO")
public class TableTwo {
#EmbeddedId
private TableTwoCompositeId tableTwoCompositeId;
#MapsId("provider") // maps provider attribute of embedded id
#ManyToOne
#JoinColumn(name = "TABLE_ONE_ID", referencedColumnName = "ID", insertable = false, updatable = false)
private TableOne tableOne;
//getters, setters, constructors
}
#Embeddable
public class TableTwoCompositeId {
#Column(name = "TABLE_ONE_ID")
public int provider;
#Column(name = "NAME")
public String name;
#Column(name = "EMAIL")
public String email;
//getters, setters, constructors
}
I have the entity following entities:
Employee extends Person
Company has List<Employee> (lazy load).
When I try to initialize the employee list with Hibernate.initialize(company.getEmployees()); I receive an error since hibernate doesn't understand that Employee is a Person.
Person.java
#Entity
#Table(name = "person")
#Inheritance(strategy = InheritanceType.JOINED)
public abstract class Person implements java.io.Serializable {
#Column(name = "person_id")
protected Long personId;
#Column(name = "name")
protected String name;
#Column(name = "age")
protected String age;
}
Employee.java
#Entity
#Table(name = "employee")
#PrimaryKeyJoinColumn(name = "person_id")
public class Employee extends Person {
#Column(name = "employee_number")
private String number;
#ManyToOne
#JoinColumn(name = "company_id")
private Company company;
}
Company.java
#Entity
#Table(name = "company")
public class Company implements java.io.Serializable {
#Column(name = "company_id")
protected String id;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "company", cascade = CascadeType.ALL)
#OrderBy(clause = "name desc")
protected List<Employee> employees = new ArrayList<Employee>();
}
Hibernate.initialize(company.getEmployees());
Exception:
org.postgresql.util.PSQLException: ERROR: column employee0_.name does not exist
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
Is there any solution with lazy load for this case?
There is certainly one to one relationship between Employee and Person. It makes sense by splitting the objects in Java but not in terms of table. I think Employee table should have a name and other columns defined in person table to simplify the design.
Suppose, we have two entities, first one:
#Entity
#Table(name = "entitya")
public class EntityA {
#Id
#Column(name = "id")
private Long id;
#Column(name = "name")
private Long name;
#OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
private Set<EntityB> childEntities;
}
and the second:
#Entity
#Table(name = "entityb")
public class EntityB {
#Id
#Column(name = "id")
private Long id;
#Column(name = "name")
private String name;
#Column(name = "master")
private Boolean master;
#ManyToOne
#JoinColumn(name = "parent")
private EntityA parent;
}
So far, so good. However underlying database tables and constrains enforce that for any entityA there can be only one EntityB with boolean field master set to true. I can extract it by adding following method to entityA:
public entityB getMasterChild() {
for(entityB ent : childEntities) {
if(ent.isMaster()) {
return ent;
}
}
}
The question is, can I create #OneToOne relationship in EntityA that can express that rule, so that entityA can have additional masterChild member of type entityB?
If I understood you correctly you want to create/define a relationship between two entities based on a value of some entity's property. The think is that relationship between entities is defined on entities count (how many entities can has the other entity) and not on some entity's property value.
However
If you really want to use #OneToOne mapping for masterChild I would recommend creating a separate table/entity for it. Once this is done, you can include this new MasterChild entity into EntityA and annotate it with #OneToOne.
Here is new MasterChild entity
#Entity
public class MasterChild extends EntityB{
#Id
#Column(name = "id")
private Long id;
}
Note that I have removed 'master' from EntityB as it is no longer needed
#Entity
#Table(name = "entityb")
public class EntityB {
#Id
#Column(name = "id")
private Long id;
#Column(name = "name")
private String name;
#ManyToOne
#JoinColumn(name = "parent")
private EntityA parent;
}
And here is modified EntityA
#Entity
#Table(name = "entitya")
public class EntityA {
#Id
#Column(name = "id")
private Long id;
#Column(name = "name")
private Long name;
#OneToOne
private MasterChild master;
#OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
private Set<EntityB> childEntities;
}
I would create table, which contains objects. Objects should displayed like columns in table. (+ public, - private)
+Company
-int companyId
-String companyName
-Set<Department> listOfDepartments = new HashSet<Department>();
+Department
-int departmentId
-String departmentName
-Set<Worker> listOfWorkers = new HashSet<Worker>();
+Worker
-int workerId
-String workerName
My unsuccessful attempt:
#XmlRootElement(name="Company")
#XmlAccessorType(XmlAccessType.FIELD)
#Entity
#Table(name="Company")
public class Company {
#Id #GeneratedValue(strategy = GenerationType.AUTO)
#XmlAttribute (name="id")
#Column (name="idCompany")
private int idCompany;
#XmlElement(name="companyName")
#Column (name="companyName")
private String companyName;
#XmlElement (name = "YYY")
#ElementCollection
private Set<Department> listOfDepartments = new HashSet<Department>();
#XmlRootElement(name="Department")
#XmlAccessorType(XmlAccessType.FIELD)
#Entity
#Embeddable
#Table(name="Department")
public class Department {
#Id #GeneratedValue(strategy = GenerationType.AUTO)
#XmlAttribute(name="idDepartment")
#Column (name="idDepartment")
private int idDepartment;
#XmlElement(name="departmentName")
#Column (name="deparmentName")
private String departmentName;
#XmlElement (name = "XXX")
#ElementCollection
private Set<Worker> listOfWorkers = new HashSet<Worker>();
#XmlRootElement(name="Worker")
#XmlAccessorType(XmlAccessType.FIELD)
#Entity
#Embeddable
#Table(name="Worker")
public class Worker {
#Id #GeneratedValue(strategy = GenerationType.AUTO)
#XmlAttribute(name="idWorker")
#Column (name = "idWorker")
private int idWorker;
#XmlElement(name="workerName")
#Column (name = "workerName")
private String workerName;
Advise the correct annotation for this situation. I will appreciate.
UPDATE:
companyId|companyName|deptId|deptName|workerId|workerNam|
1|'Lala'|1|'Logical'|1|'Jason'|
1|'Lala'|1|'Logical'|2|'Bason'|
1|'Lala'|2|'Chemical'|1|'Cason'|
1|'Lala'|2|'Chemical'|2|'Dason'|
If there is some constraint that you want to save all object(entities) in one table in your case company(but the best practice is a normal data base design you should consider company department and workers in separate tables) you have redundancy in data and your table data seems like this...
1 comp1 1 dep1 1 worker1
1 comp1 1 dep1 2 worker2
1 comp1 2 dep2 3 worker3
then correct jpa annotation is:
#XmlAccessorType(XmlAccessType.FIELD)
#Entity
#Table(name="company")
public class Company{
#Id #GeneratedValue(strategy = GenerationType.AUTO)
#XmlAttribute(name="idComapny")
#Column (name="idCompany")
private int idcompany;
#XmlElement(name="companyName")
#Column (name="companyName")
private String companyName;
**#Embedded**
private Department department;
**#Embedded**
private Worker worker;//can be removed and put in Department but result is the same
......
and put annotation #Embeddable on top of Department Entity and Worker Entity.
#XmlRootElement(name="Department")
#XmlAccessorType(XmlAccessType.FIELD)
#Entity
#Embeddable
#Table(name="Department")
public class Department {
#Id #GeneratedValue(strategy = GenerationType.AUTO)
#XmlAttribute(name="idDepartment")
#Column (name="idDepartment")
private int idDepartment;
#XmlElement(name="departmentName")
#Column (name="deparmentName")
private String departmentName;