Hibernate的配置文件的映射many-to-one错误

先看一下错误

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

虽然,这么多错误挺吓人的,还有SessionFactory为空,但是其实就一个错误

文件是这样的

文件是这样的

Customer.java

package ch06.pojo;

import java.io.Serializable;

/**
 * Customer entity. @author MyEclipse Persistence Tools
 */

public class Customer implements Serializable {

    // Fields

    private Integer id;
    private String username;
    private String password;
    private String realname;
    private String address;
    private String mobile;

    // Constructors

    /** default constructor */
    public Customer() {
    }

    /** full constructor */
    public Customer(String username, String password, String realname,
            String address, String mobile) {
        this.username = username;
        this.password = password;
        this.realname = realname;
        this.address = address;
        this.mobile = mobile;
    }

    // Property accessors

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRealname() {
        return this.realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getMobile() {
        return this.mobile;
    }

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

}

Order.java

package ch06.pojo;

import java.io.Serializable;
import java.util.Date;

/**
 * Order entity. @author MyEclipse Persistence Tools
 */

public class Order implements Serializable {

    // Fields

    private Integer id;
    private String orderno;
    private Date orderdate;
    private Double total;
    private Customer customerId;

    // Constructors

    /** default constructor */
    public Order() {
    }

    /** full constructor */
    public Order(String orderno, Date orderdate, Double total,
            Customer customerId) {
        this.orderno = orderno;
        this.orderdate = orderdate;
        this.total = total;
        this.customerId = customerId;
    }

    // Property accessors

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getOrderno() {
        return this.orderno;
    }

    public void setOrderno(String orderno) {
        this.orderno = orderno;
    }

    public Date getOrderdate() {
        return this.orderdate;
    }

    public void setOrderdate(Date orderdate) {
        this.orderdate = orderdate;
    }

    public Double getTotal() {
        return this.total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    public Customer getCustomerId() {
        return this.customerId;
    }

    public void setCustomerId(Customer customerId) {
        this.customerId = customerId;
    }

}

Customer.hbm.xml配置文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="ch06.pojo.Customer" table="customer" catalog="hibernate">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native"></generator>
        </id>
        <property name="username" type="java.lang.String">
            <column name="USERNAME" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" />
        </property>
        <property name="realname" type="java.lang.String">
            <column name="REALNAME" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        </property>
        <property name="mobile" type="java.lang.String">
            <column name="MOBILE" />
        </property>
    </class>
</hibernate-mapping>

Order.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="ch06.pojo.Order" table="order" catalog="hibernate">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="orderno" type="java.lang.String">
            <column name="ORDERNO" />
        </property>
        <property name="orderdate" type="java.util.Date">
            <column name="ORDERDATE" length="10" />
        </property>
        <property name="total" type="java.lang.Double">
            <column name="TOTAL" precision="255" scale="0" />
        </property>

         <many-to-one name="customerId" class="Customer">
            <column name="CUSTOMER_ID" />
        </many-to-one>
    </class>
</hibernate-mapping>

BussinessServer.java

package ch06.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;
import ch06.pojo.Customer;
import ch06.pojo.Order;
import ch06.util.HibernateUtils;

public class BusniessService {
    public static void main(String[] args){
        addCustomerAndOrder();
    }

    public static void addCustomerAndOrder() {
        // TODO Auto-generated method stub
        Customer customer = new Customer("zhangsan","123456","张三","青岛市","123456789");
        System.out.println("添加一条customer记录");
        addCustomer(customer);
        System.out.println("添加两条Order记录");
        Order order = new Order("1",new Date(),42.8d,customer);
        addOrder(order);
        order = new Order("2",new Date(),53.2d,customer);
        addOrder(order);
    }

    public static void addOrder(Order order) {
        // TODO Auto-generated method stub
        Session session = HibernateUtils.getSession();
        Transaction trans =session.beginTransaction();
        session.save(order);
        trans.commit();
        HibernateUtils.closeSession();
    }

    public static void addCustomer(Customer customer) {
        // TODO Auto-generated method stub
        Session session = HibernateUtils.getSession();
        Transaction trans =session.beginTransaction();
        session.save(customer);
        trans.commit();
        HibernateUtils.closeSession();
    }
}

HibernateUtils.java

package ch06.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {
    private static String CONFIG_FILE_LOCATION ="/hibernate.cfg.xml";
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private static Configuration configuration = new Configuration();
    private static SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

    static{
        try{
            configuration.configure(configFile);
            sessionFactory = configuration.buildSessionFactory();
        }catch(Exception e){
            System.err.println("creating sessionFactory");
            e.printStackTrace();
        }
    }
    private HibernateUtils(){

    }
    public static Session getSession() throws HibernateException{
        Session session = threadLocal.get();
        if(session == null  || !session.isOpen()){
            if(sessionFactory == null){
                rebuildSessionFactory();
            }
            session = (sessionFactory != null)?sessionFactory.openSession():null;
            threadLocal.set(session);
        }
        return session;
    }
    private static void rebuildSessionFactory() {
        try{
            configuration.configure(configFile);
            sessionFactory = configuration.buildSessionFactory();
        }catch(Exception e){
            System.out.println("error creating sessionFactory");
            e.printStackTrace();
        }
    }
    public static void closeSession() throws HibernateException{
        Session session = threadLocal.get();
        threadLocal.set(null);
        if(session != null){
            session.clear();
        }
    }
    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
    public static void setConfigFile(String configFile){
        HibernateUtils.configFile =configFile;
        sessionFactory = null;
    }
    public static Configuration getConfiguration(){
        return configuration;

    }
}

hibernate.cfm.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="hbm2ddl.auto">update</property>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/hibernate
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="myeclipse.connection.profile">mysqlc</property>
        <property name="show_sql">true</property>
        <mapping resource="ch06/pojo/Customer.hbm.xml" />
        <mapping resource="ch06/pojo/Order.hbm.xml" />
    </session-factory>

</hibernate-configuration>

错误:这里写图片描述

由于Transaction 在装载配置文件Order.hbm.xml文件的时候,
<many-to-one name="customerId" class="Customer">
<column name="CUSTOMER_ID" />
</many-to-one>
找不到指定的类 ,所以只需改成

<many-to-one name="customerId" class="ch06.pojo.Customer">
<column name="CUSTOMER_ID" />
</many-to-one>

就可以了,
ps:为了这个错误我差点把myeclipse卸了…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值