Guest User

Untitled

a guest
Jan 11th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. @Entity
  2. public class Apartamento implements Serializable {
  3.  
  4.  
  5. @EmbeddedId
  6. private Chave chave;
  7.  
  8. private Long andar;
  9.  
  10. private String descricao;
  11.  
  12. private BigDecimal valor;
  13.  
  14. private Long quantidade;
  15.  
  16. //Contrutores, getts e settes //
  17.  
  18. @Embeddable
  19. public class Chave implements Serializable{
  20.  
  21. private Long id;
  22. private String ala;
  23.  
  24. public Chave() {}
  25.  
  26. public Chave(Long id, String ala) {
  27. this.id = id;
  28. this.ala = ala;
  29. }
  30.  
  31. public Long getId() {
  32. return id;
  33. }
  34.  
  35. public void setId(Long id) {
  36. this.id = id;
  37. }
  38.  
  39. public String getAla() {
  40. return ala;
  41. }
  42.  
  43. public void setAla(String ala) {
  44. this.ala = ala;
  45. }
  46.  
  47.  
  48.  
  49. }
  50.  
  51. em.getTransaction().begin();
  52. Chave chave = new Chave(numero, ala);
  53.  
  54. Apartamento ap = new Apartamento(chave, andar, descricao, valor, quantidade);
  55. em.persist(ap);
  56.  
  57. em.getTransaction().commit();
  58.  
  59. public EntityManager getEM(){
  60. emf = Persistence.createEntityManagerFactory("hotel");
  61. return emf.createEntityManager();
  62. }
  63.  
  64. <?xml version="1.0" encoding="UTF-8"?>
  65. <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
  66. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
  68. http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  69. <!-- unidade de persistencia com o nome financas -->
  70. <persistence-unit name="hotel">
  71. <!-- Implementação do JPA, no nosso caso Hibernate -->
  72. <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  73. <!-- Aqui são listadas todas as entidades -->
  74. <class>entity.Apartamento</class>
  75. <class>entity.Contato</class>
  76. <class>entity.Hospede</class>
  77. <class>entity.Admin</class>
  78. <properties>
  79. <!-- Propriedades JDBC -->
  80. <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  81. <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test"/>
  82. <property name="javax.persistence.jdbc.user" value="root"/>
  83. <property name="javax.persistence.jdbc.password" value=""/>
  84. <!-- Configurações específicas do Hibernate -->
  85. <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
  86. <property name="hibernate.hbm2ddl.auto" value="update"/>
  87. <property name="hibernate.show_sql" value="true"/>
  88. <property name="hibernate.format_sql" value="true"/>
  89. </properties>
  90.  
  91. java.lang.IllegalArgumentException: Provided id of the wrong type for class entity.Apartamento. Expected: class entity.Chave, got class java.lang.Long
Add Comment
Please, Sign In to add comment