Factory error when using @Embeddable in JPA

Asked

Viewed 174 times

0

I need to create a composite key on my system, after researching a little discover that I should implement @Embeddable, even fine, I added the class on my system implementing the keys, but when I try to run the factory indicates an error related to the expected class type problem.

Entity class

@Entity
public class Apartamento implements Serializable {


@EmbeddedId
private Chave chave;

private Long andar;

private String descricao;

private BigDecimal valor;

private Long quantidade;

//Contrutores, getts e settes //

Embeddable class

@Embeddable
public class Chave implements Serializable{

private Long id;
private String ala;

    public Chave() {}

public Chave(Long id, String ala) {
    this.id = id;
    this.ala = ala;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getAla() {
    return ala;
}

public void setAla(String ala) {
    this.ala = ala;
}



}

Insertion method

 em.getTransaction().begin();
    Chave chave = new Chave(numero, ala);

    Apartamento ap = new Apartamento(chave, andar, descricao, valor, quantidade);
    em.persist(ap);

    em.getTransaction().commit();

Factory class of JPA

 public EntityManager getEM(){
    emf =  Persistence.createEntityManagerFactory("hotel");
    return emf.createEntityManager();
}

XML of my persistence

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<!--  unidade de persistencia com o nome financas  -->
 <persistence-unit name="hotel">
 <!--  Implementação do JPA, no nosso caso Hibernate  -->
 <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
 <!--  Aqui são listadas todas as entidades  -->
<class>entity.Apartamento</class>
<class>entity.Contato</class>
<class>entity.Hospede</class>
<class>entity.Admin</class>
<properties>
  <!--  Propriedades JDBC  -->
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test"/>
  <property name="javax.persistence.jdbc.user" value="root"/>
  <property name="javax.persistence.jdbc.password" value=""/>
  <!--  Configurações específicas do Hibernate  -->
  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
  <property name="hibernate.hbm2ddl.auto" value="update"/>
  <property name="hibernate.show_sql" value="true"/>
  <property name="hibernate.format_sql" value="true"/>
</properties>

Error described by the system

java.lang.IllegalArgumentException: Provided id of the wrong type for class entity.Apartamento. Expected: class entity.Chave, got class java.lang.Long

In case the error indicates exactly in em.getTransaction().begin(); that is when the connection is started so that the insertion in the bank happens.

1 answer

1

In your Key class there is no composite key, ie you do not have 2 or more id’s, else you have to identify what is key in your code, example:

@Id
private Long id;

In case you only use one key according to your code, you do not need to use @Embeddable, just instantiate the class. How are you using Hibernate, I believe you’re making trouble with @Manytoone, in that case you charge the class so:

@Entity
public class Apartamento implements Serializable {
@ManyToOne
@JoinColumn(name = "id_chave")
private Chave chave;

I advise you to take a look at relationships between entities on Hibernate.

  • in the case I am using @Embeddable why the composite key does not come from another table, so much so that in the database it is non-existent.

  • In case you are considering 'id' and 'wing' of the Key class as pk?

  • no, they are primary keys of the apartment table, the Key table does not exist

  • I get it. Take a look at this site, I believe it will help you with this problem: https://vladmihalcea.com/the-best-way-to-map-a-composite-primary-key-with-jpa-and-hibernate/

Browser other questions tagged

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