Factory error using @Embeddable in JPA

0

I need to create a composite key on my system, after researching a little to find out that I should implement @Embeddable, until all right, I added the class in my system implementing the keys, but when I try to run the factory it indicates a problem related error of the expected class type.

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();

JPA Factory Class

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

My persistence XML

<?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 the em.getTransaction().begin(); or when the connection is started for the insertion in the bank to happen.

    
asked by anonymous 11.01.2018 / 14:51

1 answer

1

In your Key class there is no compound key, ie you do not have 2 or more id's, otherwise you have to identify what is key in your code, eg

@Id
private Long id;

In case you only use a key according to your code, you do not need to use @ Embeddable , only instantiate the class. Since you are using Hibernate , I believe you are messing with ManyToOne, in which case you instantiate the class like this:

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

I would advise you take a look at the relationship between entities in hibernate .

    
11.01.2018 / 15:06