2016-11-24 158 views
-1

當我嘗試在手機上手機中的聯繫人類它提供了以下錯誤映射:如何使用hibernate映射類屬性?

Initial creation of the SessionFactory object failed. Error: org.hibernate.MappingException: Could not determine type for: com.livro.capitulo3.crudannotations.Telephone, at table: contact, for columns: [org.hibernate.mapping.Column (numPhone)] Error closing insert operation. Message: null java.lang.ExceptionInInitializerError The mapping class is as follows:

//類Contato

package com.livro.capitulo3.crudannotations; 

import java.sql.Date; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.ManyToOne; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 

import org.hibernate.annotations.ManyToAny; 

@Entity 
@Table(name = "contato") 
public class ContatoAnnotations { 

    @Id 
    @GeneratedValue 
    @Column(name = "codigo") 
    private Integer codigo; 

    @Column(name = "nome", length = 50, nullable = true) 
    private String nome; 

    @Column(name = "telefone", length = 50, nullable = true) 
    private String telefone; 

    @Column(name = "email", length = 50, nullable = true) 
    private String email; 

    @Column(name = "dt_cad", nullable = true) 
    private Date  dataCadastro; 

    @Column(name = "obs", nullable = true) 
    private String observacao; 


    //Como ficaria a annotation aqui???? Só vou persistir esta tabela 
    @OneToMany 
    private Telefone numTelefone; 
    ... 
    //Getters e Setters 

} 

//類Telefone:

package com.livro.capitulo3.crudannotations; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.ManyToOne; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 

@Entity 
@Table(name = "contato") 
public class Telefone { 
    @Id 
    @GeneratedValue 
    @Column(name = "numero") 
    private String numero; 
    @Column(name = "tipo") 
    private String tipo; 

    public String getNumero() { 
     return numero; 
    } 

    public void setNumero(String numero) { 
     this.numero = numero; 
    } 

    public String getTipo() { 
     return tipo; 
    } 

    public void setTipo(String tipo) { 
     this.tipo = tipo; 
    } 

} 

我不知道如何做這個映射。幫幫我!謝謝!!!

+0

請用英文提問。 – DimaSan

+0

不好意思!固定!這是匆忙! – Deb

+0

爲什麼你將兩個實體映射到同一個表'contato'?它應該是2個不同的表格。 – DimaSan

回答

0

如果使用@OneToMany註解,它應該是一些收集像ListSet

@OneToMany(cascade = CascadeType.ALL, mappedBy = "contacto") 
private List<Telefone> numTelefone = new ArrayList<>(); 

也爲Telephone實體更改表名稱,它必須是不同於contacto

@Entity 
@Table(name = "tel") 
public class Telefone {...} 
+0

我可不能映射班級電話嗎? – Deb

相關問題