当前位置:主页   - 电脑 - 程序设计 - JAVA
myeclipse试用小记----Hibernate多对一双向关联(2)
来源:网络   作者:   更新时间:2012-08-15
收藏此页】    【字号    】    【打印】    【关闭

  环境、数据库还和上篇文章的一样。只是表中的数据清空了。

  注意:对于有外键的表,清空数据有两种方法:第一是先drop掉外键约束后,进行清除。第二种是先清除从表数据(orders),然后清除主表的数据(customers),具体我及不说了,有空我在写写数据库外键约束的方面的文章,这点也很重要的。

  步骤

  1、清除orders、customers两表数据。

  2、用myeclipse新建web工程sx_d2y,加入hibernate支持。

  3、同时选中orders、customers生成实体Bean和mapping文件,并对mapping做小的改动。

  4、写两个测试类,分别保存Orders对象和Customers的实体对象,看看能否保存到数据库中。

  废话不说了,看过程吧!

  一、通过myeclipse生成实体和配置文件:

  Customers.java

---------------------
public class Customers implements java.io.Serializable {
  
  // Fields
  
  private Long id;
  
  private String name;
  
  private Set orderses = new HashSet(0);
  
  // Constructors
  
  /** default constructor */
  public Customers() {
  }
  
  /** full constructor */
  public Customers(String name, Set orderses) {
    this.name = name;
    this.orderses = orderses;
  }
  
  // Property accessors
  
  public Long getId() {
    return this.id;
  }
  
  public void setId(Long id) {
    this.id = id;
  }
  
  public String getName() {
    return this.name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  public Set getOrderses() {
    return this.orderses;
  }
  
  public void setOrderses(Set orderses) {
    this.orderses = orderses;
  }
  
}
  
Orders.java
---------------------
public class Orders implements java.io.Serializable {
  
  // Fields
  
  private Long id;
  
  private Customers customers;
  
  private String orderNumber;
  
  // Constructors
  
  /** default constructor */
  public Orders() {
  }
  
  /** minimal constructor */
  public Orders(Customers customers) {
    this.customers = customers;
  }
  
  /** full constructor */
  public Orders(Customers customers, String orderNumber) {
    this.customers = customers;
    this.orderNumber = orderNumber;
  }
  
  // Property accessors
  
  public Long getId() {
    return this.id;
  }
  
  public void setId(Long id) {
    this.id = id;
  }
  
  public Customers getCustomers() {
    return this.customers;
  }
  
  public void setCustomers(Customers customers) {
    this.customers = customers;
  }
  
  public String getOrderNumber() {
    return this.orderNumber;
  }
  
  public void setOrderNumber(String orderNumber) {
    this.orderNumber = orderNumber;
  }
  
}
  
Customers.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="org.lavasoft.Customers" table="customers">
    <id name="id" type="java.lang.Long">
      <column name="ID" />
  "   <generator class="increment" />
    </id>
    <property name="name" type="java.lang.String">
      <column name="NAME" length="15" />
    </property>
    <set name="orderses" inverse="true" cascade="all-delete-orphan">
      <key>
        <column name="CUSTOMER_ID" not-null="true" />
      </key>
      <one-to-many class="org.lavasoft.Orders" />
    </set>
  </class>
</hibernate-mapping>
  
Orders.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="org.lavasoft.Orders" table="orders" catalog="testdb">
    <id name="id" type="java.lang.Long">
      <column name="ID" />
      <generator class="increment" />
    </id>
    <many-to-one name="customers" class="org.lavasoft.Customers" fetch="select" cascade="save-update">
      <column name="CUSTOMER_ID" not-null="true" />
    </many-to-one>
    <property name="orderNumber" type="java.lang.String">
      <column name="ORDER_NUMBER" length="15" />
    </property>
  </class>
</hibernate-mapping>

其它资源
来源声明

版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明