Mapping this relationship with 2 entities using the same entity - mysql

I´m making the model classes using Hibernate, but I don´t know what to do with this kind of relationship.
I have three tables.
Adrress, employee and person.
One emplooye can have one adrress and one person can have one addrress.
I don´t know how map.
Because I thought to use embedded annotattion but doesn´t work.
First is to map my class, I need to put this two entities in address class?
What kind of annotattion i need to use?
I use a superclass with id property and every class extends.
I´m using mysql
my person class
#Entity
#Table(name = "destinatario")
public class Destinatario extends Persistent {
private static final long serialVersionUID = -7091318100871934315L;
#ManyToOne(cascade = CascadeType.PERSIST)
#JoinColumn(name = "endereco_id", referencedColumnName = "id")
private Endereco endereco;
#NotNull
#Size(max = 60)
#Column(name = "razao_social")
private String razaoSocial;
#NotNull
#Size(max = 14)
#Column(name = "inscricao_estadual")
private String inscricaoEstadual;
#Size(max = 9)
#Column(name = "inscricao_suframa")
private String inscricaoSuframa;
#Size(max = 60)
#Column(name = "email")
private String email;
#Size(max = 14)
#Column(name = "cnpj")
private String cnpj;
#Size(max = 11)
#Column(name = "cpf")
private String cpf;
#OneToMany
#JoinColumn(name = "destinatario_id")
private List<NotaFiscal> notaFiscais;
}
my address class
#Entity
#Table(name = "endereco")
public class Endereco extends Persistent {
private static final long serialVersionUID = -3308931308130690090L;
public enum UF {
AC("AC", "Acre"),
AL("AL", "Alagoas"),
AP("AP", "Amapá"),
AM("AM", "Amazonas"),
BA("BA", "Bahia"),
CE("CE", "Ceara"),
DF("DF", "Distrito Federal"),
ES("ES", "Espirito Santo"),
GO("GO", "Goiás"),
MA("MA", "Maranhão"),
MT("MT", "Mato Grosso"),
MS("MS", "Mato Grosso do Sul"),
MG("MG", "Minas Gerais"),
PA("PA", "Pará"),
PB("PB", "Paraíba"),
PR("PR", "Paraná"),
PE("PE", "Pernambuco"),
PI("PI", "Piauí"),
RJ("RJ", "Rio de Janeiro"),
RN("RN", "Rio Grande do Norte"),
RS("RS", "Rio Grande do Sul"),
RO("RO", "Rondônia"),
RR("RR", "Roraima"),
SC("SC", "Santa Catarina"),
SP("SP", "São Paulo"),
SE("SE", "Sergipe"),
TO("TO", "Tocantins");
private final String index;
private String descricao;
private UF(String index, String descricao) {
this.index = index;
this.descricao = descricao;
}
public String getNomeEstado() {
return descricao;
}
public String getIndex() {
return index;
}
}
#NotNull
#Size(max = 60)
#Column(name = "logradouro", unique = true)
private String logradouro;
#NotNull
#Size(max = 60)
#Column(name = "numero", unique = true)
private String numero;
#Size(max = 60)
#Column(name = "complemento")
private String complemento;
#NotNull
#Size(max = 60)
#Column(name = "bairro", unique = true)
private String bairro;
#NotNull
#Size(max = 60)
#Column(name = "municipio", unique = true)
private String municipio;
#Enumerated(EnumType.STRING)
#NotNull
//#Type(type = UFType.TYPE)
#Column(name = "uf", columnDefinition = "varchar", length = 2)
private UF uf;
#NotNull
#Size(max = 8)
#Column(name = "cep", unique = true)
private String cep;
#Size(max = 14)
#Column(name = "telefone")
private String telefone;
}
my methods to run and create a person by xml source
public static void main(String[] args) {
new Processadora().extrairDadosXml("diego");
ArquivoNotaFiscal arquivoNotaFiscal = null;
Destinatario destinatario = null;
NotaFiscal notaFiscal = null;
destinatario = createDestinatario();
arquivoNotaFiscal = createArquivoNotaFiscal();
notaFiscal = createNotaFiscal(arquivoNotaFiscal, emitente, destinatario);
destinatario.setNotaFiscais(Arrays.asList(notaFiscal));
DestinatarioDAO<Destinatario> destinatarioDAO = new DestinatarioDAOImpl<>();
Session session = HibernateSessionFactory.getSession();
Transaction transaction = session.getTransaction();
transaction.begin();
destinatarioDAO.save(destinatario);
transaction.commit();
}
private static Destinatario createDestinatario() {
Destinatario destinatario = new Destinatario();
Endereco endereco = new Endereco();
endereco.setLogradouro(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getLogradouro());
endereco.setNumero(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getNumero());
endereco.setBairro(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getBairro());
endereco.setComplemento(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getComplemento());
endereco.setCep(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getCep());
endereco.setMunicipio(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getMunicipio());
endereco.setUf(UF.valueOf(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getUF()));
endereco.setTelefone(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getTelefone());
destinatario.setEndereco(endereco);
destinatario.setRazaoSocial(nFeProc.getNfe().getInfNFe().getDestinatario().getRazaoSocial());
destinatario.setInscricaoEstadual(nFeProc.getNfe().getInfNFe().getDestinatario().getInscricaoEstadual());
destinatario.setInscricaoSuframa(nFeProc.getNfe().getInfNFe().getDestinatario().getInscricaoSuframa());
destinatario.setEmail(nFeProc.getNfe().getInfNFe().getDestinatario().getEmail());
destinatario.setCnpj(nFeProc.getNfe().getInfNFe().getDestinatario().getCnpj());
destinatario.setCpf(nFeProc.getNfe().getInfNFe().getDestinatario().getCpf());
return destinatario;
}
my database have foreign key constraint, I´m using mysql

I found my problem, I was saving only one object, because I thought if I use the save for the object who contains other it will save, but I need before save the address and later the person.
So this way everthing worked.

Related

Setting up an inheritance with Hibernate

I've been trying to set up inheritance in my Spring boot project, but I failed to do so. I've tried using the superclass mappings, joined table, single table but still I think I'm missing something. Here is how the classes look like:
Person class:
#Entity
#MappedSuperclass
#Table(name = "person")
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id_person", nullable = false)
private Integer id;
#Column(name = "first_name", nullable = false, length = 200)
private String firstName;
#Column(name = "last_name", nullable = false, length = 200)
private String lastName;
#Column(name = "PESEL", nullable = false)
private Integer pesel;
#OneToOne(fetch = FetchType.LAZY, mappedBy = "person")
private Driver driver;
//setters and getters below
}
Driver class:
#Entity
#Table(name = "driver")
public class Driver {
#Id
#Column(name = "id_driver", nullable = false)
private Integer id;
#MapsId
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "id_driver")
private Person person;
#Column(name = "birth_date", nullable = false)
private LocalDate birthDate;
#Column(name = "license", nullable = false, length = 200)
private String license;
#OneToMany(mappedBy = "idDriver")
private Set<Ride> rides = new LinkedHashSet<>();
//setters and getters below
}
Here's how it is joined in the database (mysql):
I would be very thankful if you could at least point me in the right direction (like which inheritance type will suit this simple case the best)
First remove the #Entity and #Table annotations, as well as the relationship, from your superclass:
#MappedSuperclass
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id_person", nullable = false)
private Integer id;
#Column(name = "first_name", nullable = false, length = 200)
private String firstName;
#Column(name = "last_name", nullable = false, length = 200)
private String lastName;
#Column(name = "PESEL", nullable = false)
private Integer pesel;
//setters and getters below
}
Secondly, remove the duplicated id column and remove the relationship between the mapped superclass and your derived classes, and actually extend the superclass:
#Entity
#Table(name = "driver")
public class Driver extends Person{
#Column(name = "birth_date", nullable = false)
private LocalDate birthDate;
#Column(name = "license", nullable = false, length = 200)
private String license;
#OneToMany(mappedBy = "idDriver")
private Set<Ride> rides = new LinkedHashSet<>();
//setters and getters below
}

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?

Many to many relation ship gives null

using spring data and mysql as persistence layer getting some issues in Many to many mappings
#Getter
#Setter
public class BusinessUnitEntitiy extends AbstractTenantEntity implements Auditable{
private static final long serialVersionUID = -1123383144979037984L;
#Column(name = "NAME")
String name;
#Column(name = "DESCRIPTION")
String description;
#ManyToMany(fetch = FetchType.LAZY,mappedBy = "businessUnits" )
private Set<User> businessUsers;
public Set<User> fetchBusinessUsers() {
return businessUsers;
}
#Column(name = "DISPLAY_SEQUENCE_NUM")
protected Long displaySequenceNum;
#Column(name = "UNIQUE_SEQUENCE_ID",unique = true)
protected String uniqueSequenceId;
}
#Getter
#Setter
public class User extends AbstractTenantEntity {
private static final long serialVersionUID = 65981149772133526L;
#Column(name = "PROVIDER_USER_ID")
private String providerUserId;
private String email;
#Column(name = "enabled", columnDefinition = "BIT", length = 1)
private boolean enabled;
#Column(name = "DISPLAY_NAME")
private String displayName;
private String password;
private String provider;
#Column(name = "DISPLAY_SEQUENCE_NUM")
protected Long displaySequenceNum;
#Column(name = "UNIQUE_SEQUENCE_ID",unique = true)
protected String uniqueSequenceId;
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(
name = "business_unit_user",
joinColumns={#JoinColumn(name ="user_id")},
inverseJoinColumns = { #JoinColumn(name="business_unit_id") }
)
Set<BusinessUnitJpaEntitiy> businessUnits;
}
fetching the user from businessunit works perfectly
but fetching businessunits from users gives null set even updating the same user is persisiting only the newly linked businessunit older values vanishes
If you persisted the user within the transaction without initializing the businessUnits fields, that's what you get. Either you also initialize the set correctly before persisting, or you detach the user after persisting, so that the user is reloaded from the database and the set is properly initialized.
you can try this
#ManyToMany
#JoinTable(
name = "business_unit_user",
joinColumns={#JoinColumn(name ="user_id",referencedColumnName = "id")},
inverseJoinColumns = { #JoinColumn(name="business_unit_id",referencedColumnName = "id") }
)
Set<BusinessUnitJpaEntitiy> businessUnits;
and you must have setter and getter for each property

data is posting as anonymous user in the data base when posting from postman

This is my entity class where it is having createdBy and lastModifiedBy fields. When ever I'm posting the data from postman for the fields 'createdBy' and 'lastModifiedBy' the columnn in the database is saving as anynomous user instead of the name which is posting from the postman
#Slf4j
#Getter
#Setter
#MappedSuperclass
#EntityListeners(AuditingEntityListener.class)
public abstract class AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
//#CreatedBy
#Column(name = "created_by", nullable = false, length = 50, updatable = false)
//#JsonIgnore
private String createdBy;
#CreatedDate
#Column(name = "created_date", updatable = false)
#JsonIgnore
private Instant createdDate = Instant.now();
//#LastModifiedBy
#Column(name = "last_modified_by", length = 50)
//#JsonIgnore
private String lastModifiedBy;
#LastModifiedDate
#Column(name = "last_modified_date")
#JsonIgnore
private Instant lastModifiedDate = Instant.now();
From the UserRequestDTO it is posting the request which is given below
UserRequestDTO.class
#Data
public class UserRequestDTO {
#NotBlank
#ApiModelProperty(required = true, name = "UserName must be between 3 and 255 characters long", position = 0)
#Size(min = 3, max = 255)
private String userName;
private NameDTO name;
#NotBlank
#ApiModelProperty(required = true, name = "displayName must be between 3 and 100 characters long")
#Size(min = 3, max = 100)
private String displayName;
#ApiModelProperty(example = "1")
private Long organizationId;
#Lob
private String photo;
#ApiModelProperty(example = "India")
private String country;
#ApiModelProperty(example = "bhargav")
private String createdBy;
#ApiModelProperty(example = "bhargav")
private String lastModifiedBy;
}
}
This is the data which I'm trying to post from the post man.
{
"userName": "jaya",
"displayName": "jay",
"country":"USA",
"createdBy":"bhargav",
"lastModifiedBy":"jaya krishna",
"phoneNumber":"9876543210",
"email":"jaya#abc.com",
"startDate":"2019-12-08",
"endDate":"2020-01-08"
}
But the createdBy column and the lastModified column is saving as the anynomous user instead of the name which is posting from the POSTMAN. datatype of both the columns is varchar.
Can you please share the User Entity witch you are trying to persist in the Database, also if you can share how you are mapping DTO with Entity Object.

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...