Problems with JPA, @Onetoone and mappedBy

Asked

Viewed 313 times

2

I have three classes, Person, Client and Address, being Customer Person’s child and Address adding Person.

Segue Pessoa:

@Entity
public abstract class Pessoa implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String nome;
private String cpf;
private String email;
private String pw;
private String numeroEnd;
private String complementoEnd;

@OneToOne
public Endereco endereco;

Follows Address:

@Entity
public class Endereco implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)    
private long   id;
private String cep;
private String logradouro;
private String bairro;
private String cidade;
private String uf;

@OneToOne(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private Pessoa pessoa;

The mistake I’m having is this::

Exception Description: An Exception was thrown while Searching for persistence Archives with Classloader: Webappclassloader (delegate=true; repositories=WEB-INF/classes/) Internal Exception: javax.persistence.Persistenceexception: Exception [Eclipselink-28018] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.Entitymanagersetupexception Exception Description: Predeployment of Persistenceunit [Lojagamespu] failed. Internal Exception: Exception [Eclipselink-7154] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.Validationexception Exception Description: The attribute [pessoa] in Entity class [class br.com.lojagames.model.Endereco] has a mappedBy value of [post] which does not exist in its Owning Entity class [class br.com.lojagames.model.Pessoa]. If the Owning Entity class is a @Mappedsuperclass, this is invalid, and your attribute should Reference the correct subclass.

I am a layman in JPA but I would like to do to gain experience, however I believe I did the relationship between the classes in JPA in the wrong way, could point out me the error?

  • @Exactly, I did and it worked, thank you very much, Paul, it helped me a lot!

1 answer

1


The attribute [pessoa] in Entity class [class br.com.lojagames.model.Endereco] has a mappedBy value of [post] which does not exist in its Owning Entity class [class br.com.lojagames.model.Pessoa]. If the Owning Entity class is a @Mappedsuperclass, this is invalid, and your attribute should Reference the correct subclass.

The parameter mappedBy should be used when there is a bidirectional relationship, as is your case. However, it should "map" the class that is calling it.

@OneToOne(mappedBy = "endereco", cascade = CascadeType.ALL, orphanRemoval = true)
  • Paul, it worked perfectly, thank you very much! Without abusing your patience, after I register a zip code with the client the following error occurs: java.lang.Illegalstateexception: During Synchronization a new Object was found through a Relationship that was not marked Cascade PERSIST: br.com.lojagames.model.Address@4afb0d29. You know what can be?

  • 1

    For a bidirectional relation the two classes must be updated, for this you must use the CascadeType.PERSIST instead of ALL, in case it becomes difficult to understand or does not solve, ball a new question with the details that the community will help you again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.