【问题标题】:How to map a class attribute using hibernate?如何使用休眠映射类属性?
【发布时间】:2017-04-09 04:23:08
【问题描述】:

当我尝试将电话映射到联系人类中的电话时,会出现以下错误:

SessionFactory 对象的初始创建失败。错误:org.hibernate.MappingException:无法确定类型:com.livro.capitulo3.crudannotations.Telephone,在表:联系人,列:[org.hibernate.mapping.Column(numPhone)] 关闭插入操作时出错。消息:空 java.lang.ExceptionInInitializerError 映射类如下:

//类联系方式

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

}

//班级电话:

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;
    }

}

我不知道如何进行此映射。帮助!谢谢!!!

【问题讨论】:

  • 请用英语提问。
  • 对不起!固定的!很着急!
  • 为什么要将两个实体映射到同一张表contato?它应该是 2 个不同的表。
  • 这个想法是只映射到联系人表。
  • 如果你想将两个实体映射到同一个表而不是阅读这个问题,有答案:stackoverflow.com/questions/29007676/…

标签: java hibernate mapping hibernate-mapping


【解决方案1】:

因为您没有正确映射它。有几种方法可以做到:https://en.m.wikibooks.org/wiki/Java_Persistence/OneToMany

【讨论】:

    【解决方案2】:

    如果你使用@OneToMany注解,它应该是一些像ListSet这样的集合:

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

    还要更改Telephone实体的表名,它必须不同于contacto

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

    【讨论】:

    • 我不能映射班级电话吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 2012-04-09
    • 2013-02-09
    • 1970-01-01
    相关资源
    最近更新 更多