hibernate关系映射----多对一单向关联

hibernate关系映射----多对一单向关联

一个客户可以对应多个订单。

表结构:两张表使用powerdesiner设计生成,产生引用错误。

出现问题:1.定义的级联关系只安一中方式生成,即双向的所以删除了parent表的set属性,同时删除映射文件的set集合,

               2.没有定义级联关系cascade属性

 

drop table if exists Customer1;

drop table if exists OrderC1;

/*==============================================================*/
/* Table: Customer1                                             */
/*==============================================================*/
create table Customer1
(
   id                   int not null auto_increment,
   name                 varchar(20),
   primary key (id)
)
type = InnoDB;

/*==============================================================*/
/* Table: OrderC1                                               */
/*==============================================================*/
create table OrderC1
(
   id                   int not null,
   order_id             varchar(20),
   customer_id          int not null,
   primary key (id)
)
type = InnoDB;

alter table OrderC1 add constraint FK_Reference_1111 foreign key (customer_id)
      references Customer1 (id) on delete restrict on update restrict;

注意:因为有外键约束,需要事务支持,在安装数据库的时候,需要配置mysql数据库服务器的参数。数据库的引擎应该用InnoDB

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

package many_one;

@SuppressWarnings("serial")
public class Customer1  implements java.io.Serializable {
     private Integer id;
     private String name;
    public Customer1() {
    }
    public Customer1(String name ) {
        this.name = name;
    }

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

    public String getName() {
        return this.name;
    }
   
    public void setName(String name) {
        this.name = name;
    }
}

 

@SuppressWarnings("serial")
public class Orderc1  implements java.io.Serializable {

     private Integer id;
     private Customer1 customer1;
     private String orderId;

    public Orderc1() {
    }

    public Orderc1(Customer1 customer1) {
        this.customer1 = customer1;
    }
   
    public Orderc1(Customer1 customer1, String orderId) {
        this.customer1 = customer1;
        this.orderId = orderId;
    }
    public Integer getId() {
        return this.id;
    }
   
    public void setId(Integer id) {
        this.id = id;
    }

    public Customer1 getCustomer1() {
        return this.customer1;
    }
   
    public void setCustomer1(Customer1 customer1) {
        this.customer1 = customer1;
    }

    public String getOrderId() {
        return this.orderId;
    }
   
    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }
}

配置文件

Customer1.hbm.xml

<hibernate-mapping>
    <class name="many_one.Customer1" table="customer1" catalog="test1" >
        <id name="id" type="integer" >
            <column name="id" not-null="false"/>
            <generator class="increment" />
        </id>
        <property name="name" type="string">
            <column name="name" length="20" />
        </property>
    </class>
</hibernate-mapping>

 

Orderc1.hbm.xml

<hibernate-mapping>
    <class name="many_one.Orderc1" table="orderc1" catalog="test1">
        <id name="id" type="integer">
            <column name="id" />
            <generator class="increment" />
        </id>
        <many-to-one name="customer1" class="many_one.Customer1" fetch="select" cascade="save-update">
            <column name="customer_id" not-null="true" />
        </many-to-one>
        <property name="orderId" type="string">
            <column name="order_id" length="20" />
        </property>
    </class>
</hibernate-mapping>

package many_one;

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

public class tests {

 /**
  * @param args
  */
 public static void main(String[] args) {

  Configuration cfg = new Configuration().configure();
  SessionFactory sf  =cfg.buildSessionFactory();
  Session session =sf.openSession();
  
  Transaction ts = session.beginTransaction();
  
  Customer1 c = new Customer1("c1");
  Orderc1 o = new Orderc1();
  o.setOrderId("23");
  
  o.setCustomer1(c);
  
  session.save(o);
  
  ts.commit();
 
 }


}

你可能感兴趣的:(hibernate关系映射----多对一单向关联)