Objectify embedded array inside an embedded array can't persist - java

My Class heirarchy is as follows
School - contains list of Employees - which contains list of qualifications
Employees is an Embedded list in School. I can persist a School with it's employees no problem. Now when I add the list of qualifications to an employee as an embedded field I get the following error
You cannot nest multiple #Embedded arrays or collections
The objectify documentation seems to indicate I should be able to do this provided the objects are serializable which they are. Am I missing something? If this is the way it works is there a way around it?
Update:
School Class
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Embedded;
import javax.persistence.Id;
import com.googlecode.objectify.annotation.Entity;
#Entity
#SuppressWarnings("serial")
public class School implements Serializable
{
#Id
private String title;
#Embedded
private List<Employee> employees = new ArrayList<Employee>();
public School ()
{
}
public School (String title)
{
this.title = title;
}
public void addEmployee( Employee employee )
{
this.employees.add(employee);
}
}
Employee Class
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Embedded;
import javax.persistence.Id;
import com.googlecode.objectify.annotation.Entity;
#Entity
#SuppressWarnings("serial")
public class Employee implements Serializable
{
#Id
private String title;
#Embedded
private List<String> qualifications = new ArrayList<String>();
public Employee ()
{
}
public Employee (String title)
{
this.title = title;
}
public void addQualification( String qualification )
{
this.qualifications.add(qualification);
}
}
Caused by: java.lang.IllegalStateException: You cannot nest multiple #Embedded arrays or collections. A second was found at private java.util.List com.app.nquizitive.shared.Employee.qualifications
at com.googlecode.objectify.impl.save.EmbeddedMultivalueFieldSaver.<init>(EmbeddedMultivalueFieldSaver.java:36)
at com.googlecode.objectify.impl.save.EmbeddedCollectionFieldSaver.<init>(EmbeddedCollectionFieldSaver.java:21)
at com.googlecode.objectify.impl.save.ClassSaver.<init>(ClassSaver.java:64)
at com.googlecode.objectify.impl.save.EmbeddedMultivalueFieldSaver.<init>(EmbeddedMultivalueFieldSaver.java:43)
at com.googlecode.objectify.impl.save.EmbeddedCollectionFieldSaver.<init>(EmbeddedCollectionFieldSaver.java:21)
at com.googlecode.objectify.impl.save.ClassSaver.<init>(ClassSaver.java:64)
at com.googlecode.objectify.impl.save.ClassSaver.<init>(ClassSaver.java:29)
at com.googlecode.objectify.impl.Transmog.<init>(Transmog.java:322)
at com.googlecode.objectify.impl.ConcreteEntityMetadata.<init>(ConcreteEntityMetadata.java:75)
at com.googlecode.objectify.impl.Registrar.register(Registrar.java:69)
at com.googlecode.objectify.ObjectifyFactory.register(ObjectifyFactory.java:209)
at com.googlecode.objectify.ObjectifyService.register(ObjectifyService.java:38)
at com.app.nquizitive.server.dao.SchoolDao.<clinit>(SchoolDao.java:12)

There are two different annotations:
#Embed (#Embedded in ofy3)
#Serialize (#Serialized in ofy3)
If you want something to serialize, use the second. If you want something embedded, use the first. You can't nest #Embed(ded) lists, but you can put a #Serialize(d) list inside an embedded list.

Which of the classes above are annotated with #Entity? It sounds like School is a datastore entity, while Employees are not (i.e. they are just serialized into School) and qualifications are not (i.e. they are just serialized into Employees).
The Objectify annotation of #Embedded isn't needed/relevant/allowed, in a non-Entity class.

Related

Problem while populating child's id with parent's id in Hibernate JPA

i'm not a professional in spring boot / hibernate jpa, so i apologize in advance.
I have 2 Entities, one called Document and the other called Bgo. Document is the parent table and Bgo the child. So my objective is to create the Document and after that, Bgo will receive it's id, for example:
When i create the first Document, it will have id_document = 1, so after that, the Bgo will have id_document = 1 as well.
I want to populate the child's id with the parent's id.
Here is the Parent Entity:
package com.testing.testing.models;
import java.io.Serializable;
import java.sql.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
#Entity
#Table(name = "DOCUMENT")
public class Document implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id_document;
private int num_document;
private Date date;
#OneToOne(mappedBy = "document", cascade = CascadeType.ALL)
private Bgo bgo;
public Document() {
}
public Document(Bgo bgo) {
this.id_document = bgo.getId_document();
this.bgo = bgo;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Bgo getBgo() {
return bgo;
}
public void setBgo(Bgo bgo) {
this.bgo = bgo;
}
public long getId_document() {
return id_document;
}
public void setId_document(long id_document) {
this.id_document = id_document;
}
public int getNum_document() {
return num_document;
}
public void setNum_document(int num_document) {
this.num_document = num_document;
}
}
And Child Entity:
package com.testing.testing.models;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
#Entity
#Table(name = "BGO")
public class Bgo implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private long id_document;
private String name_bgo;
private int num_bgo;
#OneToOne
#PrimaryKeyJoinColumn(name = "id_document", referencedColumnName = "id_document")
private Document document;
public Bgo() {
}
public Bgo(Document document) {
this.id_document = document.getId_document();
this.document = document;
}
public long getId_document() {
return id_document;
}
public void setId_document(long id_document) {
this.id_document = id_document;
}
public String getName_bgo() {
return name_bgo;
}
public void setName_bgo(String name_bgo) {
this.name_bgo = name_bgo;
}
public int getNum_bgo() {
return num_bgo;
}
public void setNum_bgo(int num_bgo) {
this.num_bgo = num_bgo;
}
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
}
I used the term PrimaryKeyJoinColumn cause i want Bgo's id to be the Document's id, thats why i also used the same name "id_document". So Bgo's id will be Primary and Foreign at the same time (if it is wrong please tell me a better way to do it, knowledge is always welcoming)
I have also Document's Repository:
package com.testing.testing.repository;
import com.testing.testing.models.Document;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DocumentRepository extends JpaRepository<Document, Long> {
}
And Bgo's:
package com.testing.testing.repository;
import com.testing.testing.models.Bgo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BgoRepository extends JpaRepository<Bgo, Long> {
}
Document's Controller:
package com.testing.testing.controllers;
import java.util.List;
import com.testing.testing.models.Document;
import com.testing.testing.repository.DocumentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping(value="/document")
public class DocumentController {
#Autowired
DocumentRepository documentRepository;
#GetMapping
public List<Document> listDocument() {
return documentRepository.findAll();
}
#PostMapping
public Document createDocument(#RequestBody Document document) {
return documentRepository.save(document);
}
}
Bgo's Controller:
package com.testing.testing.controllers;
import java.util.List;
import com.testing.testing.models.Bgo;
import com.testing.testing.repository.BgoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping(value="/bgo")
public class BgoController {
#Autowired
BgoRepository bgoRepository;
#GetMapping
public List<Bgo> listBgo() {
return bgoRepository.findAll();
}
}
So basically, the problem is this:
enter image description here
When i create a new Document and also the Bgo object, Bgo's id is 0, as you can see in the image, the object Bgo has the "id_document = 0 ", shouldn't it be id_document = 1 ?
And when i try to list Bgo, it still shows id_document = 0:
enter image description here
That's how i want the database to be:
enter image description here
As you can see, they are different tables. But Document has the primary key and is the parent, and Bgo is a child cause it is receiving Document's id. As you can see, Bgo's has the Document's id, it is primary and foreign at the same time, thats why i used PrimaryKeyJoinColumn. Both of them have the same id, Document's id equals Bgo's id. So whenever i create a Document and a Bgo at the same time, both of them should have the same id.
you might need to use the inheritance functionality comes with Spring instead. have a look at this:
https://www.baeldung.com/hibernate-inheritance
you can for instance use the #Inheritance(strategy = InheritanceType.SINGLE_TABLE)
on your parent table. and then you will annotate the child with just the #Entity
and then you will extend it as you normally do in a normal Inheritance scenario
so your parent class will look like so:
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.Table;
import javax.persistence.InheritanceType;
#Entity
#Table(name = "Document")
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Document {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "Id")
private Integer Id;
#Column(name = "num_document")
private int num_document;
#Column(name = "date")
private Date date;
public Document() {
}
public Document( int num_document, Date date) {
super();
this.num_document = num_document;
this.date = date;
}
}
and the child will look like so
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.Table;
#Entity
#Table(name = "Bgo")
public class Bgo extends Document {
#Column(name = "name_bgo")
private String name_bgo;
#Column(name = "num_bgo")
private int num_bgo;
public Bgo(String name_bgo, int num_bgo) {
super();
this.name_bgo = name_bgo;
this.num_bgo = num_bgo;
}
public Bgo() {
super();
}
}
and you will have One JPARepository which looks like so;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DocumentRepository extends JpaRepository<Document, Integer> {
}
the result of this code, will generate one table in your database with one extra column dtype which will identify the record to be either Bgo or any other type you might need to add in the future. this is the idea behind inheritance anyway
I hope this helped
Here are the three different inheritance strategy you have using spring
Single mapping is the default mapping
And it uses #inheritance(strategy = InheritanceType.SINGLE_TABLE) annotation to the parent class
In this strategy, the parent class is a table and all its children will be specified in a discriminator column in the parent table. The column called dtype and it contain the name of the entity as a value.
Table per the class strategy is similar to the superclass strategy but the superclass is also an entity you need to avoid this one if you want to make so many join queries
Joined table strategy is used the same as above. In this strategy, the subclasses and the superclass will all be tables in the database, but the subclasses will not inherit the filed of the superclass, it is useful if we want to apply data integrity and null constrains on some field
for more details look here
https://thorben-janssen.com/complete-guide-inheritance-strategies-jpa-hibernate/
choose which one will suit your case and then you can use the implementation I provided in the first answer and you only need to change the this annotation
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)

Spring Boot: Prevent persisting field declared in superclass

I am creating a Todo app in Spring Boot and I need to create two tables: Task and Todo(Todo extends Task).
In Task table is a field called description and I would like to prevent that column to be created in Todo table.
How can I do it?
Task(parent):
package com.example.todo.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
#Entity
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Task {
#Id
private long id;
private String name;
private String description;
}
Todo(child):
package com.example.todo.model;
import javax.persistence.Entity;
import javax.persistence.Transient;
#Entity
public class Todo extends Task {
private boolean isChecked;
}
I would suggest you clean up your design because concrete classes inheriting from other concrete classes is (often) a code smell. The proper solution to this is to factor out the common parts of both classes into a (abstract) super class and then add the specific fields to the concrete inheriting classes:
#Entity
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Completable {
#Id
private long id;
private String name;
}
#Entity
public class Task extends Completable {
private String description;
}
#Entity
public class Todo extends Completable {
private boolean isChecked;
}
so you have the behaviour grouped in the classes where it belongs and don't have to make sure that one thing contains a description while it shouldn't.
What you want cannot be done easily. But you might be trying to solve an issue in the wrong way.
From what I am reading you have a Task entity with has two separate types:
one with a checkbox indicating its completion
one with an additional description
If this is the case you might want to model the classes the same way. Thus having:
A Task entity without the description
A Todo entity extending Task with the checkbox
A new SummaryTask extending Task with a description field

Spring/JPA/Hibernate How to Perform Join of Two Entities In One Repository

First let's say we have two tables. One table is an Employee table with the following columns:
EMPLOYEE:
------------------------
emp_id (int, primary key)
emp_name (varchar(125))
emp_dept (foreign key)
emp_intro (text)
The other table is a Department table with the following columns:
DEPARTMENT:
-----------
dept_id (int, primary key)
dept_label (varchar(25))
Here is a sample of the table's values
DEPARTMENT:
------------------------
dept_id | dept_label
------------------------
1 | Sales
------------------------
2 | Technology
------------------------
3 | Finance
In order to return the employee's info with a status label, we need to either perform a JOIN:
SELECT e, d.dept_label FROM employees JOIN department d ON d.dept_id = e.emp_dept
or a multi-table select:
SELECT e.emp_id, e.emp_name, d.dept_label, e.emp_intro FROM employees e, department d WHERE e.emp_dept = d.dept_id
However, when using JPA/Hibernate, we need to create two classes:
Employee.java
package com.example.entities;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name = "employees")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "emp_id")
private long emp_id;
#Column(name = "emp_name")
private String emp_name;
#Column(name = "emp_dept")
private Integer emp_dept;
#Column(name = "emp_intro")
private String emp_intro;
public long getEmp_id() {
return emp_id;
}
public void setEmp_id(long emp_id) {
this.emp_id = emp_id;
}
public String getEmp_name() {
return emp_name;
}
public void setEmp_name(String emp_name) {
this.emp_name = emp_name;
}
public Integer getEmp_dept() {
return emp_dept;
}
public void setEmp_dept(Integer emp_dept) {
this.emp_dept = emp_dept;
}
public String getEmp_intro() {
return emp_intro;
}
public void setEmp_intro(String emp_intro) {
this.emp_intro = emp_intro;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
Department.java
package com.example.entities;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name = "departments")
public class Department implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "dept_id")
private long dept_id;
#Column(name = "dept_label")
private String dept_label;
public long getDept_id() {
return dept_id;
}
public void setDept_id(long dept_id) {
this.dept_id = dept_id;
}
public String getDept_label() {
return dept_label;
}
public void setDept_label(String dept_label) {
this.dept_label = dept_label;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
Then, there is the repository (DAO):
EmployeeRepository
package com.example.repository;
import.java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.example.entities.Employee;
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
#Query("select e, d.dept_label FROM Employee e JOIN Department d ON "
+ "d.dept_id = e.emp_id")
public List<Employee> return getEmployees();
}
and lastly, the Java controller that binds the classed query to an endpoint of the application:
EmployeeController.java
package com.example.controllers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.entities.Department;
import com.example.entities.Employee;
import com.example.repository.EmployeeRepository;
#Controller
public class EmployeeController {
#Autowired
EmployeeRepository er;
#RequestMapping(value = "/getEmployees")
public #ResponseBody List<Employee> getEmployees() {
return er.getEmployees();
}
}
I have already tested this entire structure with only retrieving rows inside of the Employee table (i.e. #Query("SELECT e FROM Employee e") ) and everything returns as is.
MY MAIN ISSUE is how does one return a JOIN QUERY while the query is inside of a specific class (table), being Employee, if I require contents inside of Department?
I've already tried #JoinColumn annotations and that didn't work as well (perhaps I did it wrong).
Any ideas? Thanks.
You dont have to use raw joins to do that, just use proper relation mapping. Relation between Employee and Departament sounds like #ManyToOne or #ManyToMany.
You will be able to eg employee.getDepartament() or query by employee.departament.name=:name
http://www.objectdb.com/api/java/jpa/ManyToMany
You can even map bidirectional relations so you will be able to get deparament from employee, as well as all employees from given deparaments
PS. #JoinColumn is used to delare DB columnt used for joins it it is different then created by selected named strategies (usualy entityname_id). Actual relation mapping is done by declaring #OneToOne #OneToMany #ManyToMany and those can but doesn't have to be used with #JoinColumn. It is strict JPA question.
Here you have complete documentation of JPA 2.1 specification
It describes in details how to declare relations as well as #MappedSuperclass, inheritance strategies and all other usefull stuff.

Insert to DataBase Using EntityManager

I've been looking for a way to interact with my database using EntityManager class in Java.
The question is related to these two tables I have defined in my DB:
PARENT_TABLE:
PK_ONE
PK_TWO
CHILD TABLE:
PK_ONE
COLUMN
PK_TWO
Here is something I have so far.
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
import javax.persistence.PrimaryKeyJoinColumn;
#Entity
#Table(name="PARENT_TABLE")
#SecondaryTable(name="CHILD_TABLE", pkJoinColumns={
#PrimaryKeyJoinColumn(name="PK_ONE"),
#PrimaryKeyJoinColumn(name="PK_TWO")})
public class ExampleTbl implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name="PK_ONE")
private String pkOne;
#Id
#Column(name="COLUMN", table="CHILD_TABLE")
private String column;
#Column(name="PK_TWO")
private String pkTwo;
//GETTERS AND SETTERS
}
My questions are:
To be able to Insert to the PARENT_TABLE a new row, do I have to create a new Entity class with just two fields (PK_ONE, PK_TWO) to be able to use the merge() or persist() method?
Will I have to create another Entity class to Insert a new row to my CHILD_TABLE?
To retrieve a List with the existing data I have a method something like this:
#Transactional(readOnly = true)
public List<ExampleTbl> getFoldersList() {
em = emf.createEntityManager();
Query q = em.createQuery("SELECT e FROM ExampleTbl e WHERE e.pkTwo = :pkTwo ORDER BY e.pkOne");
q.setParameter("pkTwo", "My Test");
List<ExampleTbl> result = q.getResultList();
return result;
}
Do you think this is the best way to do it?
I think its a question with a couple of possible answers. I would recommend reviewing some of the ways they set things up in the Java EE 6 tutorial examples. You can read about that at http://docs.oracle.com/javaee/6/tutorial/doc/giqst.html

PlayFramework: CRUD and sorted lists

I've just read about CRUD system of PlayFramework and decided to write simple example, just two classes Group and User and decided that a lot of users can be in one group but one user can be just in one group. So, I've used for it ManyToOne and OneToMany anotations, please take a look to the code below:
package models;
import play.db.jpa.Model;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
/**
* #author sergiizagriichuk
*/
#Entity
public class User extends Model {
public String firstName;
public String lastName;
#ManyToOne
public Group group;
#Override
public String toString() {
return firstName;
}
}
and
package models;
import play.db.jpa.Model;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import java.util.List;
/**
* #author sergiizagriichuk
*/
#Entity
public class Group extends Model {
public String groupName;
public String groupDescription;
#OneToMany
#OrderBy("firstName desc")
public List<User> users;
}
For sorting I've tried to use OrderBy annotation, and as result nothing :(, I have list of users in additional sorting mode, but I wanna order by firstName, Could someone explain me how to sort list using controller for friendly using in CRUD mode ?
Thanks.
did you try adding the mappedBy attribute in your users list?
#OneToMany(mappedBy="group")

Categories