java - kejuruteraan hadapan hibernate menjana model perhubungan satu-ke-banyak tanpa ralat, tetapi hanya satu jadual dijana
我想大声告诉你
我想大声告诉你 2017-05-17 10:04:01
0
1
578

Saya kini akan melakukan kejuruteraan hadapan, hubungan satu-ke-banyak antara pelanggan Pengguna dan pesanan Pesanan

Berikut adalah kelas entiti mereka

package com.cw.entity;

import java.util.Set;

public class User implements java.io.Serializable {
    private Integer id;
    private String username;
    private String password;
    private Set<Order> order;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Set<Order> getOrder() {
        return order;
    }
    public void setOrder(Set<Order> order) {
        this.order = order;
    }
    public User(String username, String password, Set<Order> order) {
        super();
        this.username = username;
        this.password = password;
        this.order = order;
    }
    
    
}

package com.cw.entity;

public class Order implements java.io.Serializable {
    private Integer id;
    private User user;
    private String shop;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public String getShop() {
        return shop;
    }
    public void setShop(String shop) {
        this.shop = shop;
    }
    public Order(User user, String shop) {
        super();
        this.user = user;
        this.shop = shop;
    }
    
}

Berikut ialah fail konfigurasi hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.cw.entity.User" table="user" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="username" type="java.lang.String">
            <column name="username" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" />
        </property>
        <set name="order" cascade="save-update" inverse="true">
            <key column="order"></key>
            <one-to-many class="com.cw.entity.Order"/>
        </set>
    </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.cw.entity.Order" table="order" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <!-- <property name="user" type="com.cw.entity.User">
            <column name="user" />
        </property> -->
        <property name="shop" type="java.lang.String">
            <column name="shop" />
        </property>
        <many-to-one name="user" class="com.cw.entity.User" cascade="all" column="user"></many-to-one>
    </class>
</hibernate-mapping>

Saya telah menyemak konfigurasi lain dan tiada masalah, hibernate.cfg.xml juga baik (dua item konfigurasi model entiti dan konfigurasi hbm2ddl telah ditambah), tetapi pelaksanaannya

package com.cw.entity;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.id.Configurable;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Configuration configure = new Configuration().configure();
        SessionFactory buildSessionFactory = configure.buildSessionFactory();
        Session session = buildSessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        tx.commit();
    }

}

Selepas itu, hanya jadual pengguna dijana, tetapi jadual pesanan tidak dijana Apakah masalahnya sekarang?

我想大声告诉你
我想大声告诉你

membalas semua(1)
过去多啦不再A梦

Gunakan anotasi, anotasi popular sekarang

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan