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.
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.
– Juliano Silveira
In case you are considering 'id' and 'wing' of the Key class as pk?
– Edjane
no, they are primary keys of the apartment table, the Key table does not exist
– Juliano Silveira
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/
– Edjane