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.