JPA EclipseLink oneToMany Derived Ids Fail - mysql

Hello I'm trying to make an example of persistence of a OneToMany relationship in which I get the following error:
Exception Description: Invalid composite primary key specification. The names of the primary key fields or properties in the primary key class [entitys.OrderItemPK] and those of the entity bean class [class entitys.OrderItem] must correspond and their types must be the same. Also, ensure that you have specified ID elements for the corresponding attributes in XML and/or an #Id on the corresponding fields or properties of the entity class.
Note: I'm using EclipseLink and MySQL DB
The entities:
#Entity
public class CustomerOrder implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "idOrder")
private Integer idOrder;
#Basic(optional = false)
#Column(name = "orderText")
private String orderText;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "customerOrder")
private Collection<OrderItem> orderItemCollection;
public CustomerOrder() {
}
}
#Entity
#IdClass(OrderItemPK.class)
public class OrderItem implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
protected OrderItemPK orderItemPK;
#Basic(optional = false)
#Column(name = "itemDesc")
private String itemDesc;
#Id
#ManyToOne(optional = false)
#JoinColumns({
#JoinColumn(name="idOrder", referencedColumnName="idOrder"),
#JoinColumn(name="ItemNumber", referencedColumnName="ItemNumber")
})
//#JoinColumn(name = "idOrder", referencedColumnName = "idOrder", insertable = false, updatable = false)
private CustomerOrder customerOrder;
private CustomerOrder customerOrder;
public OrderItem() {
this.orderItemPK = new OrderItemPK();
}
}
#Embeddable
public class OrderItemPK implements Serializable {
#Basic(optional = false)
#Column(name = "idOrder")
private int idOrder;
#Basic(optional = false)
#Column(name = "itemNumber")
private int itemNumber;
public OrderItemPK() {
}
}
The test Source:
public static void main(String[] args) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
// Fill the items fileds
OrderItem item = new OrderItem();
item.setItemDesc("Item Text");
// Fill the orders fields
CustomerOrder order = new CustomerOrder();
order.setOrderText("Order text");
// Fill the relationship fields
order.getOrderItemCollection().add(item);
item.setCustomerOrder(order);
em.persist(order);
em.getTransaction().commit();
}
I have no idea what I'm doing wrong, any suggestions are welcome.

Well i managed to solve the problem by removing idClass annotation and adding the #MapId
heres the final code:
#Entity
#Table(name = "Orders")
public class CustomerOrder implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "idOrder")
private Integer idOrder;
#Basic(optional = false)
#Column(name = "orderText")
private String orderText;
#OneToMany(mappedBy = "customerOrder", cascade = CascadeType.ALL)
private Collection<OrderItem> orderItemCollection;
public CustomerOrder() {
}
#Entity
#Table(name = "OrdersItems")
public class OrderItem implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
protected OrderItemPK orderItemPK;
#Basic(optional = false)
#Column(name = "itemDesc")
private String itemDesc;
#MapsId("idOrder")
#ManyToOne(optional = false)
#JoinColumn(name = "idOrder", referencedColumnName = "idOrder", nullable = false)
private CustomerOrder customerOrder;
public OrderItem() {
this.orderItemPK = new OrderItemPK();
}
#Embeddable
public class OrderItemPK implements Serializable {
#Basic(optional = false)
#Column(name = "idOrder")
private int idOrder;
#Basic(optional = false)
#Column(name = "itemNumber")
private int itemNumber;
public OrderItemPK() {
}
public static void main(String[] args) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
// Fill the order item
OrderItem item = new OrderItem();
item.getOrderItemPK().setItemNumber(1);
item.setItemDesc("Product Name");
// Fill the order
CustomerOrder order = new CustomerOrder();
order.setOrderText("Testing");
// Create relationship
order.getOrderItemCollection().add(item);
item.setCustomerOrder(order);
// Persist
em.persist(order);
em.getTransaction().commit();
}
But i can't get it work with IdClass, here's the source
#Embeddable
public class OrderItemPK implements Serializable {
#Basic(optional = false)
#Column(name = "idOrder")
private Integer idOrder;
#Basic(optional = false)
#Column(name = "ItemNumber")
private Integer itemNumber;
public OrderItemPK() {
}
#Entity
#IdClass(OrderItemPK.class)
public class OrderItem implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name = "itemNumber")
private Integer itemNumber;
#Basic(optional = false)
#Column(name = "itemDesc")
private String itemDesc;
#MapsId("idOrder")
#ManyToOne(optional = false)
#JoinColumn(name = "idOrder", referencedColumnName = "idOrder", nullable = false)
private CustomerOrder customerOrder;
public OrderItem() {
}
#Entity
public class CustomerOrder implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "idOrder")
private Integer idOrder;
#Basic(optional = false)
#Column(name = "OrderText")
private String orderText;
#OneToMany(mappedBy = "customerOrder", cascade = CascadeType.ALL)
private Collection<OrderItem> orderItemCollection;
public CustomerOrder() {
this.orderItemCollection = new ArrayList();
}

Use MapsId in OrderItem:
#Entity
public class OrderItem implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
protected OrderItemPK orderItemPK;
#Basic(optional = false)
#Column(name = "itemDesc")
private String itemDesc;
#MapsId("idOrder")
#ManyToOne(optional = false)
#JoinColumn(name = "idOrder", referencedColumnName = "idOrder")
private CustomerOrder customerOrder;
}
An alternative is to remove the embedded id and use an ID class:
#Entity
#IdClass(OrderItemPK.class)
public class OrderItem implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name = "itemNumber")
private int itemNumber;
#Basic(optional = false)
#Column(name = "itemDesc")
private String itemDesc;
#Id
#ManyToOne(optional = false)
#JoinColumn(name = "idOrder", referencedColumnName = "idOrder")
private CustomerOrder customerOrder;
}
public class OrderItemPK implements Serializable {
//name of the relation in the entity, but same type as the CustomerOrder PK.
int customerOrder;
int itemNumber;
public OrderItemPK() {
}
}

Related

Cant remove parent row in MysqlDatabase by JavaSpring

I have problem with deleting datas in my Tables.
CategoryModel:
#Entity
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "Category")
public class CategoryModel {
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Id
private Long category_id;
#Column(name = "CategoryName")
private String cateGoryName;
#Column(name = "Image")
#Lob
private String image;
#OneToMany(mappedBy = "categoryModel", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ProductModel> productModels = new ArrayList<>();
}
ProductModel:
#Entity
#Getter
#Setter
#Table(name = "Products")
public class ProductModel {
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Id
private Long product_id;
#Column(name = "ProductName")
#Lob
private String productName;
#Column(name = "ShortName")
private String shortName;
#Column(name = "Price")
private int price;
#Column(name = "Slider")
private Boolean slider;
#Column(name = "SpecialOffer")
private Boolean specialOffer;
#Column(name = "NewPrice")
private int newPrice;
#Column(name = "ShortDesc")
private String shortDesc;
#Column(name = "FullDescription")
#Lob
private String fullDescription;
#Column(name = "Image")
#Lob
private String image;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "category_model_category_id")
private CategoryModel categoryModel;
}
And even i have addnotation: "orphanRemoval = true" i still cant remove category when a product is assigned to it. Error looks like:
"#1451 - Cannot delete or update a parent row"
Can someone tell me how could i improve it?

How can i mapping entities in spring boot jpa?

I'm new in Spring Boot JPA
I have questions in JPA Entity mappings.
there is 4 tables in my MySql DB
SPACE, PROJECT, ISSUE, MEMBER
SPACE is Big Project which contains multiple PROJECT.
PROJECT contains multiple ISSUE.
and MEMBER can join only 1 SPACE and multiple PROJECT which MEMBER belongs to SPACE.
MEMBER can write multiple ISSUE
in this situation, my ERD model is correct?
my ERD
and please check my jpa mappings.
If there's anything wrong, please point it out.
SPACE
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "space_no")
private Long spaceNo;
#NotEmpty
#Column(name = "space_name", unique=true, length = 100)
private String spaceName;
/** 1:N relation */
#OneToMany(mappedBy = "smsSpace")
private List<PmsProject> pmsProjects = new ArrayList<>();
PROJECT
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "project_no")
private Long projectNo;
#Column(name ="space_no")
private Long spaceNo;
#Column(name = "project_name", length = 100)
private String projectName;
/** 1:N relation */
#OneToMany(mappedBy = "pmsProject")
private List<ImsIssue> imsIssues = new ArrayList<>();
#OneToMany(mappedBy = "pmsProject")
private List<PmsProjectMember> projectMembers = new ArrayList<>();
/** N:1 relation */
#ManyToOne
#JoinColumn(name = "space_no", referencedColumnName = "space_no", insertable = false, updatable = false)
private SmsSpace smsSpace;
MEMBER
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "member_no")
private Long memberNo;
#Column(name = "mail_address", unique=true, length = 100)
private String mailAddress;
#Column(name = "name", length = 100)
private String name;
#Column(name = "keyword", length = 1000)
private String keyword;
#Column(name = "image", length = 1000)
private String image;
#Column(name = "password", length = 1000)
private String password;
#Column(name = "user_id", length = 50)
private String userId;
#Enumerated(EnumType.STRING)
private MemberRole role;
public void encodingPassword(String password) {
this.password = password;
}
/** 1:N realtion */
#OneToMany(mappedBy = "mmsMember")
private List<PmsProjectMember> projectMembers = new ArrayList<>();
ISSUE
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "issue_no")
private Long issueNo;
#Column(name ="project_no")
private Long projectNo;
#Column(name = "issue_name", length = 1000)
private String issueName;
#Column(name = "priority")
private Long priority;
#Column(name = "status", length = 20)
private String status;
#Column(name = "summary", length = 100)
private String summary;
#Column(name = "is_overdue")
private Long isOverdue;
#Column(name = "issue_type_cd")
private String issueTypeCd;
/** N:1 relation */
#ManyToOne
#JoinColumn(name = "project_no", referencedColumnName = "project_no", insertable = false, updatable = false)
private PmsProject pmsProject;
PROJECTMEMBER
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "group_no")
private Long groupNo;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "member_no")
private MmsMember mmsMember;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "project_no")
private PmsProject pmsProject;
I've been thinking about it for days, but I can't solve it because I lack knowledge. Please help me.
Assuming I got your situation right, you have A member that can have one Space and multiple project, space has multiple projects, every project can have more than one issue, every member can write more then one issue for each project.
Due to the suggestion the ERD you posted it's not corrected.
Here is the correct ERD
(I just wrote the Foreign Keys and Primary Keys, the rest its up to you)
And here you have all the entites:
Member
#Entity
#Table(name = "MEMBERS")
public class Member {
//members is the property name in Project entity.
#ManyToMany(mappedBy = "members")
Set<Project> projects;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY, generator = "native")
#Column(name = "MEMBER_ID")
private Long id;
#ManyToOne
#JoinColumn(name = "SPACE_ID")
private Space space;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Space getSpace() {
return space;
}
public void setSpace(Space space) {
this.space = space;
}
public Set<Project> getProjects() {
return projects;
}
public void setProjects(Set<Project> projects) {
this.projects = projects;
}
}
Space
#Entity
#Table(name = "SPACES")
public class Space {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY, generator = "native")
#Column(name = "SPACE_ID")
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Issue
#Entity
#Table(name = "ISSUES")
public class Issue {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY, generator = "native")
#Column(name = "ISSUE_ID")
private Long id;
#ManyToOne
#JoinColumn(name = "MEMBER_ID")
private Member member;
#ManyToOne
#JoinColumn(name = "PROJECt_ID")
private Project project;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Member getMember() {
return member;
}
public void setMember(Member member) {
this.member = member;
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
}
Project
#Entity
#Table(name = "PROJECTS")
public class Project {
#ManyToMany
#JoinTable(
name = "PROJECTS_MEMBERS",
joinColumns = #JoinColumn(name = "PROJECT_ID"),
inverseJoinColumns = #JoinColumn(name = "MEMBER_ID"))//Is referring to the id of the other Entity, in this case, members
Set<Member> members;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY, generator = "native")
#Column(name = "PROJECT_ID")
private Long id;
#ManyToOne
#JoinColumn(name = "SPACE_ID")
private Space space;
public Set<Member> getMembers() {
return members;
}
public void setMembers(Set<Member> members) {
this.members = members;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Space getSpace() {
return space;
}
public void setSpace(Space space) {
this.space = space;
}
}
You don't have to put necessarily both #ManyToOne and #OneToMany annotation, the back-reference could be useful in some use case, you have to see if you need it or not. Remember the back reference could cause problems with deserialization, creating a stack overflow by circular reference. You can avoid this, using transient keyword or various annotation (depending on the library you are using, Jackson, Gson, ecc..).
Be careful to don't use FetchType.EAGER randomly here's the why => Difference between FetchType LAZY and EAGER in Java Persistence API?

Nested one to many relationship jpa spring boot

I have three classes - Document, Page, Sentence. A Document will have multiple Pages & each Page will have multiple Sentences. I'm trying to map One to Many relationship using Spring Data JPA annotation. But it only works when there are only one layer like - Document>Page. Doesn't work while it's Document>Page>Sentence.
Can anyone please give me a solution for how to do it for nested one to many relationship ?
My classes are given below.
#Entity
#Table(name = "DOCUMENT")
public class Document implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name = "FILEID")
#GeneratedValue(strategy = GenerationType.AUTO)
private long idFile;
#Lob
#Column(name = "CONTENT")
private byte[] content;
#Column(name = "NAME")
private String name;
#Column(name = "ID_MAIL_USER")
private String idMailUser;
#Column(name = "NUM_PAGES")
private int numPages;
#Column(name = "TO_ANALIZE")
private boolean toAnalize;
#Column(name = "HASH")
private String hash;
#Column(name = "EXTENSION")
private String extension;
#Column(name = "SIZE")
private double size;
#Column(name = "LINK_TO_DRIVE_FILE")
private String linkToDriveFile;
#Column(name="PATH")
private String path;
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#Column(name = "PAGES")
private List<Page> pages = new ArrayList<>();
// Setter Getters
}
.
#Entity
#Table(name = "PAGE")
public class Page implements Serializable {
#Id
#Column(name = "PAGE_ID")
#GeneratedValue(strategy = GenerationType.AUTO)
private long idPage;
#Column(name = "PAGE_NUMBER")
private int pageNum;
#Lob
#Column(name = "CONTENT")
private String content;
#ManyToMany(cascade = CascadeType.ALL)
#Column(name = "SENTENCES")
private List<Sentence> sentences = new ArrayList<>();
// Setter Getters
}
.
#Entity
#Table(name = "SENTENCE")
public class Sentence implements Serializable {
//private long idFile;
//private long idPage;
#Id
#Column(name = "SENTENCE_ID")
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#Column(name = "PAGE_NUMBER")
private int pageNumber;
#Column(name = "ORDER")
private int ord;
#Column(name = "CONTENT")
private String content;
#Column(name = "HASH")
private String hash;
// Setter Getters
}
Your OneToMany mappings are incorrect. Correct it as follows
#Entity
#Table(name = "DOCUMENT")
public class Document implements Serializable {
......
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name = "DOCUMENT_ID") //Name the foreign key column in PAGE table to DOCUMENT_ID
private List<Page> pages = new ArrayList<>();
}
#Entity
#Table(name = "PAGE")
public class Page implements Serializable {
....
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name = "PAGE_ID") //Name the forein key column in PAGE table to PAGE_ID
private List<Sentence> sentences = new ArrayList<>();
}
Use #JoinColumn annotation instead of #Column to give the name of the foreign key that do the physical mapping between tables in your database.
#Entity
#Table(name = "DOCUMENT")
public class Document implements Serializable {
private static final long serialVersionUID = 1L;
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy="document")
#Column(name = "PAGES")
private List<Page> pages = new ArrayList<>();
}
#Entity
#Table(name = "PAGE")
public class Page implements Serializable {
#ManyToOne
#JoinColumn(name="DOCUMENT_ID")
private Document document;
#ManyToMany(cascade = CascadeType.ALL, mappedBy="pages")
#Column(name = "SENTENCES")
private List<Sentence> sentences = new ArrayList<>();
}
#Entity
#Table(name = "SENTENCE")
public class Sentence implements Serializable {
#ManyToMany(mappedBy="sentences")
private List<Page> pages;
}
Here a Document One to Many relationship with Pages.
So.. we need define mappedBy in the entity we want to map another entity.. so in this case
#OneToMany(mappedBy="document",cascade = CascadeType.ALL, orphanRemoval = true)
and in referenced entity i.e. Pages we want foreign key DOCUMENT_ID, so we have to define it using
#ManyToOne
#JoinColumn(name="DOCUMENT_ID")

Many to many relationships doesn't shown in JSON. Spring + JPA

The relationships works fine, the tables are created and the data is inserted correctly. But when page loads in browser, many to many relationships doesn't shown.
But professions are not null:
Here is my entities:
#Entity
#Table(name = "users")
public class User {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "login", nullable = true, length = 50)
private String login;
#Column(name = "pass", nullable = true, length = 50)
private String password;
#Column(name = "name", nullable = false, length = 50)
private String name;
....
public User() {
}
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(name="worker_profession", joinColumns =#JoinColumn(name="id_user", referencedColumnName="id"),
inverseJoinColumns = #JoinColumn(name="id_profession", referencedColumnName="id")
)
private Set<Profession> profession;
public Set<Profession> getProfession() {
return profession;
}
public void setProfession(Set<Profession> profession) {
this.profession = profession;
}
...
}
And:
#Entity
#Table(name = "profession")
public class Profession {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#Column(name = "profession_name", nullable = false, length = 50)
private String professionName;
public Profession() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getProfessionName() {
return professionName;
}
public void setProfessionName(String professionName) {
this.professionName = professionName;
}
#ManyToMany(fetch = FetchType.EAGER, mappedBy = "profession")
private Set<User> users;
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
in UserController.class I have:
#RestController
#RequestMapping("/user")
public class UserController {
#Autowired
private UserRepository userRepository;
#RequestMapping(value = "", method = RequestMethod.GET)
#ResponseBody
public List<User> getAllUsers() {
return userRepository.findAll();
}
...
}
So, where is my mistake? Thanks.

Gson StackOverflow on JSONWriter

I created a system and it libero access via Webservice Restful, with him I release a maintenance list, this list is formed by objects: Cliente, Endereco, Manutencao, StatusManutencao.
I have a class that seeks the bench all maintenance that is Class: PesquisaManutencoesWebService, and the class that receives the call webservice calls SimpleRestService.
When I turn my maintenance list in JSON (return gson.toJson (ws.getJson ());), is generated the following error:
java.lang.StackOverflowError
java.io.StringWriter.write (StringWriter.java:112)
com.google.gson.stream.JsonWriter.string (JsonWriter.java:559)
com.google.gson.stream.JsonWriter.writeDeferredName (JsonWriter.java:402)
com.google.gson.stream.JsonWriter.value (JsonWriter.java:495)
com.google.gson.internal.bind.TypeAdapters $ 8.write (TypeAdapters.java:268)
Already tested if the list is working, and really is, I can get all the data I want, but I just can not turn into JSON.
Can anyone help me? Thanks.
Class Cliente
#Entity
#Table(name = "cliente")
public class Cliente implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue
private Long id;
#NotBlank #Size(max = 6)
#Column(nullable = false, length = 6)
private String codigoAcesso;
#NotBlank #Size(max = 100)
#Column(nullable = false, length = 100)
private String nome;
#Size(max = 150)
#Column(nullable = true, length = 150)
private String email;
#NotBlank #Size(max = 14)
#Column(name = "doc_receita_federal", nullable = false, length = 14)
private String documentoReceitaFederal;
#Enumerated(EnumType.STRING)
#Column(nullable = false, length = 15)
private TipoPessoa tipo;
#OneToMany(mappedBy = "cliente", cascade = CascadeType.ALL)
private List<Endereco> enderecos = new ArrayList<>();
Class Endereco
#Entity
#Table(name = "endereco")
public class Endereco implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue
private Long id;
#NotBlank #Size(max = 150)
#Column(nullable = false, length = 150)
private String logradouro;
#NotBlank #Size(max = 20)
#Column(nullable = false, length = 20)
private String numero;
#Size(max = 100)
#Column(length = 100)
private String complemento;
#NotBlank #Size(max = 100)
#Column(nullable = false, length = 100)
private String cidade;
#NotBlank #Size(max = 100)
#Column(nullable = false, length = 100)
private String uf;
#NotBlank #Size(max = 8)
#Column(nullable = false, length = 8)
private String cep;
//#NotBlank
#ManyToOne
#JoinColumn(name = "cliente_id", nullable = false)
private Cliente cliente;
getters and setters ...
Class Manutencao
#Entity
#Table(name = "manutencao")
public class Manutencao implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue
private Long id;
#ManyToOne
#JoinColumn(name = "cliente_id")
private Cliente cliente;
#Embedded
private StatusManutencao status;
#NotBlank
#Column(nullable = false, length = 255, name="descricao_manutencao")
private String descricaoManutencao;
#Column(name = "valor", precision = 10, scale = 2)
private BigDecimal valor;
getters and setters ...
Class StatusManutencao
#Embeddable
public class StatusManutencao implements Serializable {
private static final long serialVersionUID = 1L;
#Column(name="recebido")
#Type(type="true_false")
private boolean recebido;
#Temporal(TemporalType.DATE)
#Column(name="data_recebimento")
private Date dataRecebimento;
#Column(name="em_manutencao")
#Type(type="true_false")
private boolean emManutencao;
#Temporal(TemporalType.DATE)
#Column(name="data_manutencao")
private Date dataManutencao;
#Column(name="manutencao_finalizada")
#Type(type="true_false")
private boolean manutencaoFinalizada;
#Temporal(TemporalType.DATE)
#Column(name="data_finalizacao")
private Date dataFinalizacao;
#Column(name="entregue")
#Type(type="true_false")
private boolean entregue;
#Temporal(TemporalType.DATE)
#Column(name="data_entrega")
private Date dataEntrega;
#Column(name="pago")
#Type(type="true_false")
private boolean pago;
#Temporal(TemporalType.DATE)
#Column(name="data_pagamento")
private Date dataPagamento;
getters and setters ...
Class PesquisaManutencaoWebService
public class PesquisaManutencoesWebService implements Serializable {
private static final long serialVersionUID = 1L;
private EntityManager manager;
private EntityManagerFactory factory;
private EntityTransaction trx;
public PesquisaManutencoesWebService(){
this.factory = Persistence.createEntityManagerFactory("ManutencaoPU");
this.manager = factory.createEntityManager();
this.trx = manager.getTransaction();
this.trx.begin();
}
#SuppressWarnings("unchecked")
public JsonModel getJson(){
List<Manutencao> manutencoes = manager.createQuery("SELECT m FROM Manutencao m").getResultList();
return new JsonModel(manutencoes);
}
}
Class SimpleRestService
#Path("service")
public class SimpleRestService {
private PesquisaManutencoesWebService ws;
#GET
#Produces("application/json")
#Path("/{codigoAcesso}/")
public String getManutencoes(#PathParam("codigoAcesso") String codigoAcesso) throws IOException, SQLException, ClassNotFoundException{
ws = new PesquisaManutencoesWebService();
Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().setPrettyPrinting().create();
return gson.toJson(ws.getJson());
}
}
In most of the cases StackOverError is thrown when call stack exceeds because of excessive deep or infinite recursion. It is also thrown when the need to store the local variable in the method exceeds the allocated stack size.
In your Endereco Class, add Fetch Type to #ManyToOne anotattion in the Cliente attribute, then your code should look like this:
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "cliente_id")
private Cliente cliente;
When you set FetchType to Lazy, you are basically saying that Entity should not be nested to the parent Entity.
Then try to get the Clientes List Manually...