Hibernate中缺少列错误?

时间:2015-03-02 10:36:55

标签: java hibernate hibernate-mapping hibernate-criteria

我在Hibernate中丢失了列错误。整个错误是这样的,org.hibernate.HibernateException:缺少列:在vaccum.enquiry中的enquiry_type_enquiry_type_id。这是我的代码。

package com.beans;

import java.sql.Timestamp;

import javax.management.loading.PrivateClassLoader;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;


@Entity
@Table(name="enquiry")
public class Enquiry {


    @Id@GeneratedValue
    @Column(name="enquiry_id")
    int enquiry_id;

    @Column(name="text")
    String text;

    @Column(name="location")
    String location;

    @Column(name="name")
    String name;

    @Column(name="mobile")
    String mobile;


    @Column(name="date_time")
    Timestamp date_time;


    /////////////////////


    @ManyToOne(cascade = CascadeType.ALL)
    private Enquiry_Type enquiry_type;

    public Enquiry_Type getEnquiry_type() {
        return enquiry_type;
    }

    public void setEnquiry_type(Enquiry_Type enquiry_type) {
        this.enquiry_type = enquiry_type;
    }


    //////////////////////////
    public Enquiry() {
        // TODO Auto-generated constructor stub
    }

    public Enquiry(String text,String location,String name,String mobile,Timestamp date_time) {

        this.text    = text;
        this.location = location;
        this.name = name;
        this.mobile = mobile;
        this.date_time = date_time;
    }

    public int getEnquiry_id() {
        return enquiry_id;
    }

    public void setEnquiry_id(int enquiry_id) {
        this.enquiry_id = enquiry_id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public Timestamp getDate_time() {
        return date_time;
    }

    public void setDate_time(Timestamp date_time) {
        this.date_time = date_time;
    }

}

Enquiry_Type.java在这里,

package com.beans;

import java.sql.Timestamp;

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

@Entity
@Table(name="enquiry_type")
public class Enquiry_Type {

    @Id@GeneratedValue
    @Column(name="enquiry_type_id")
    int enquiry_id;

    @Column(name="enquiry_type")
    String enquiry_type;

    public Enquiry_Type() {

    }

    public Enquiry_Type(String enquiry_type) {

        this.enquiry_type    = enquiry_type;

    }

    public String getEnquiry_type() {
        return enquiry_type;
    }

    public void setEnquiry_type(String enquiry_type) {
        this.enquiry_type = enquiry_type;
    }



}

我从SessionFactory factory=cfg.buildSessionFactory();收到错误。该代码如下所示,

//creating configuration object
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file

//creating seession factory object
SessionFactory factory=cfg.buildSessionFactory();
//creating session object
Session session=factory.openSession();

//creating transaction object
Transaction t=session.beginTransaction();

表-1:查询


enquiry_id |文字|位置|名字|手机|类型| date_time |

表-2:Enquiry_Type


enquiry_type_id | enquiry_type |

此处,Enquiry.type = Enquiry_Type.enquiry_type_id

3 个答案:

答案 0 :(得分:1)

如何生成数据库架构?如果你使用hibernate来做,你应该将hibernate.hbm2ddl.auto属性的值更改为“create”或“create-drop”。

答案 1 :(得分:0)

你可以交叉检查你是否有一个名为" enquiry_type_enquiry_type_id"在表格"查询"。

您必须确保表中存在的所有列都应存在于实体Class

答案 2 :(得分:0)

首先确保您的要求,如果您想通过cascade属性将Parent元素与其新的OneToMany子元素一起插入,请使用下面的。

@OneToMany(cascade = CascadeType.ALL)
private Enquiry_Type enquiry_type;

注释@JoinColumn表示该实体是该关系的所有者,即相应的表具有一个列,该列具有引用表的外键)

@ManyToOne
@JoinColumn(name = "enquiryTypeID")
private Enquiry_Type enquiry_type;