Orgnization.java
package com.chk.model;import java.util.HashSet;
import java.util.Set;import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;@Entity
public class Orgnization {
private int id;
private String name;
private String sn;
private String description;
private Orgnization parent;// 父节点
private Set<Person> persons = new HashSet<Person>();// 子节点 @Id
@GeneratedValue
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSn() {
return sn;
} public void setSn(String sn) {
this.sn = sn;
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
} @ManyToOne
public Orgnization getParent() {
return parent;
} public void setParent(Orgnization parent) {
this.parent = parent;
} @OneToMany(mappedBy = "org")
public Set<Person> getPersons() {
return persons;
} public void setPersons(Set<Person> persons) {
this.persons = persons;
}}Person.java
package com.chk.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;@Entity
public class Person {
private int id;
private String name;
private String sex;
private String address;
private String duty;
private String phone;
private String description;
private Orgnization org; @ManyToOne
public Orgnization getOrg() {
return org;
} public void setOrg(Orgnization org) {
this.org = org;
} @Id
@GeneratedValue
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getDuty() {
return duty;
} public void setDuty(String duty) {
this.duty = duty;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
}
}
一、我要用Annotation的方式,
二、我现在只能通过 “子”找到“父”
问题是:我如何通过 “父”找到“子”我的测试时代码如下:
test.java
@Test
public void loadtest() {
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.getCurrentSession();
s.beginTransaction(); Orgnization org1 = (Orgnization) s.get(Orgnization.class, 1);
Set set = org1.getPersons();
System.out.println(set.size());
for (Iterator iterator = set.iterator(); iterator.hasNext();) {
Orgnization org = (Orgnization) iterator.next();
System.out.println(org.getName());

} s.getTransaction().commit(); }

解决方案 »

  1.   


    一、我要用Annotation的方式,
    二、我现在只能通过 “子”找到“父”
    问题是:我如何通过 “父”找到“子”
      

  2.   

    父类里面怎么还有个父节点呢,还对自己设置了个ManyToOne
      

  3.   


    如何通过“父”找到“子”?
    你说呢?
    从你的配置上看,注解配置,不太规范,应该给表名,列名(不给就是默认属性值),不太规范而已。不过配置来看,大体是没有问题。
    你的父是父组织吗?你父组织配置的是自关联,如果你想实现双相关联,并且是双相关联,则还需要加一个子组织的set。
    不加 @JoinColumn()会生成中间表。
     
      

  4.   

    你能告诉我给怎样加@JoinColumn()吗
    需要哪些参数
      

  5.   

    双向关联package com.zuxiang.entity;import java.util.List;import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;import org.directwebremoting.annotations.DataTransferObject;
    /**
     * CusCustomer entity. @author MyEclipse Persistence Tools
     */
    @Entity
    @Table(name = "customer" ,schema = "cc")public class Customer implements java.io.Serializable { // Fields /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "cus_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long cusId;
    @Column(name = "cus_name" ,length = 20 )
    // @RemoteProperty 如果只要该属性s
    private String cusName;
    @Column(name = "cus_addr",length = 250)
    private String cusAddr;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "customer" ,fetch = FetchType.EAGER)
    private List<Product> products; // Constructors /** default constructor */
    public Customer() {
    } /** full constructor */
    public Customer(String cusName, String cusAddr) {
    this.cusName = cusName;
    this.cusAddr = cusAddr;
    } // Property accessors public Long getCusId() {
    return this.cusId;
    } public void setCusId(Long cusId) {
    this.cusId = cusId;
    } public String getCusName() {
    return this.cusName;
    } public void setCusName(String cusName) {
    this.cusName = cusName;
    } public String getCusAddr() {
    return this.cusAddr;
    } public void setCusAddr(String cusAddr) {
    this.cusAddr = cusAddr;
    }

    public List<Product> getProducts() {
    return products;
    } public void setProducts(List<Product> products) {
    this.products = products;
    }}
    /**
     * 
     */
    package com.zuxiang.entity;import java.io.Serializable;import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;import org.directwebremoting.annotations.DataTransferObject;/**
     * @author zuxiang
     *
     */
    @Entity
    @Table(name = "product" ,schema = "cc")
    public class Product implements Serializable{ /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "pro_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long proId;

    @Column(name = "pro_name" , length = 50 ,nullable = false)
    private String proName;

    @Column(name = "pro_price" ,nullable = false)
    private Double proPrice;

    @Column(name = "pro_description" ,length = 500 )
    private String proDescription;

    @ManyToOne(cascade = CascadeType.ALL )
    @JoinColumn(name = "customer_id")
    private Customer customer;


    public Double getProPrice() {
    return proPrice;
    }
    public void setProPrice(Double proPrice) {
    this.proPrice = proPrice;
    } public Long getProId() {
    return proId;
    }
    public void setProId(Long proId) {
    this.proId = proId;
    }
    public String getProName() {
    return proName;
    }
    public void setProName(String proName) {
    this.proName = proName;
    } public String getProDescription() {
    return proDescription;
    }
    public void setProDescription(String proDescription) {
    this.proDescription = proDescription;
    }


    public Customer getCustomer() {
    return customer;
    }
    public void setCustomer(Customer customer) {
    this.customer = customer;
    }

    }
      

  6.   

    这不是 JPA 么?哪是什么 Hibernate 哦。PS:建议实体类和数据表中不要设置任何关联约束
      

  7.   

    organization自关联@ManyToOne  
        @JoinColumn(name = "parent_id")  
        private Orgnization parent;  
      
        @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")  
        private Set<Orgnization> children = new HashSet<Orgnization>(); 
      

  8.   


    您的意思是再增加一个属性children ,用它来记录父机构的记录?