日期:2014-05-20  浏览次数:20758 次

hibernate注解配置集合映射list
set搞了一个上午,list弄了一下午了,注解都不知道怎么弄
Java code

package gpf;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;

@Entity
public class Husband {
    private int id;
    private String name;
    private List<Wifes> wifes=new ArrayList<Wifes>();
    
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public void setId(int id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    @OneToMany(mappedBy="husband",cascade=CascadeType.ALL)
    @OrderBy("number desc")
    public List<Wifes> getWifes() {
        return wifes;
    }
    public void setWifes(List<Wifes> wifes) {
        this.wifes = wifes;
    }
}


Java code
package gpf;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Wifes {
    private int id;
    private String name;
    private int number;
    private Husband husband;
    
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="husb_id")
    public Husband getHusband() {
        return husband;
    }
    public void setHusband(Husband husband) {
        this.husband = husband;
    }
    
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public void setId(int id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
}


XML code

<?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">

<hibernate-configuration>
    <session-factory>
    
        <!--<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:example</property>
        <property name="connection.username">scott</property>
        <property name="connection.password">tiger</property>
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
    
        -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        
        <property name="connection.pool_size">1</property>
        
        <property name="current_session_context_class">thread</property>

        <property name="format_sql">true</property>
        <property name="show_sql">true</property>

        <mapping class="gpf.Husband"/>
        <mapping class="gpf.Wifes"/>
    </session-factory>
</hibernate-configuration>