自己理解:

单向1对多。
一个客户可以发出多个订单。但是一个订单只属于一个客户。
写对象的时候。在多的那个类对象把1的作为自己的一个属性。
写配置文件
<many-to-one name=1的属性名。class=1的类名。column=1的主键名>

多的1方的Java类。 把1的作为一个属性放到多的里面。

package com.hibernate.n21;

public class Order {
private Integer orderId;
private String orderName;
private Customer customer;
private Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Order(String orderName, Customer customer) {
super();
this.orderName = orderName;
this.customer = customer;
} }

映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hibernate.n21"> <class name="Order" table="orders" > <id name="orderId" type="java.lang.Integer">
<column name="order_Id" />
<!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
<generator class="native" />
</id> <property name="orderName"
type="java.lang.String" column="order_Name" >
</property> <!-- 映射n-1的关联关系
        name:1的属性名。把1当成多的一个属性。
        class 1的类名。
        column 1的主键名。
-->
<many-to-one name="customer" class="Customer" column="customer_Id"></many-to-one>
</class> </hibernate-mapping>

1的类名:

package com.hibernate.n21;

public class Customer {
private Integer customerId;
private String customerName; public Integer getCustomerId() {
return customerId;
} public void setCustomerId(Integer customerId) {
this.customerId = customerId;
} public String getCustomerName() {
return customerName;
} public void setCustomerName(String customerName) {
this.customerName = customerName;
} public Customer() {
super();
} public Customer(String customerName) {
super();
this.customerName = customerName;
} }

映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hibernate.n21"> <class name="Customer" table="customer" > <id name="customerId" type="java.lang.Integer">
<column name="customer_Id" />
<!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
<generator class="native" />
</id> <property name="customerName"
type="java.lang.String" column="customer_Name" >
</property> </class> </hibernate-mapping>

然后在主的映射文件hibernate.cfg.xml文件中加上映射

     <mapping resource="com/hibernate/n21/Customer.hbm.xml"/>
<mapping resource="com/hibernate/n21/Order.hbm.xml"/>

SAVE():

 先保存1的一端  只是三条insert语句。
@org.junit.Test
public void testN21Save(){
Customer customer=new Customer();
customer.setCustomerName("C1");
Order order1 =new Order();
order1.setOrderName("O1");
Order order2 =new Order();
order2.setOrderName("O2"); //设置关联关系。就是给多的一方设置外键
order1.setCustomer(customer);
order2.setCustomer(customer);
session.save(customer);
session.save(order1);
session.save(order2); }
 先保存多的一端。3条insert语句。2条update语句
因为在插入多的一端时,无法确定1的一端的外键值。所以只能先为null。等1的一端插入后。再进行修改。
@org.junit.Test
public void testN21Save(){
Customer customer=new Customer();
customer.setCustomerName("C1");
Order order1 =new Order();
order1.setOrderName("O1");
Order order2 =new Order();
order2.setOrderName("O2"); //设置关联关系。就是给多的一方设置外键
order1.setCustomer(customer);
order2.setCustomer(customer); session.save(order1);
session.save(order2);
session.save(customer);
}

select()方法:

如果在第5行关闭session。就会出现懒加载异常。因为System.out.println(order.getCustomer().getClass());  class com.hibernate.n21.Customer_$$_javassist_0
得到的是一个customer代理对象。
1 @org.junit.Test
public void testN21Select(){
Order order=(Order) session.get(Order.class, 5);
System.out.println(order.getOrderName());
session.close();
Customer customer = order.getCustomer();
System.out.println(customer.getCustomerName());
}

对于懒加载异常。我们可以在hibernate.cfg.xml中设置立即加载。

<class name="Customer" table="customer" lazy="false">    我们设置懒加载为否。就是立即加载

Update():

@org.junit.Test
public void testN21Update(){
Order order=(Order) session.get(Order.class, 5);
Customer customer=order.getCustomer();
customer.setCustomerName("N21Update");
}

Delete:

//删除多的,直接删除
@org.junit.Test
public void testN21Delete(){
Order order=(Order) session.get(Order.class, 5);
session.delete(order);
}
//不设置级联删除的话。删除1的一方。会出现异常。因为1的一方。对应好几个  
//但是1的一方没有被引用的话。是可以删除的 1 @org.junit.Test
public void testN21Delete(){
Customer customer=(Customer) session.get(Customer.class, 3);
session.delete(customer);
}
org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3343)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3546)
at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:100)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:293)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at com.hibernate.n21.Test.destroy(Test.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:37)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`luwei`.`orders`, CONSTRAINT `FK_qaw4j85b8ppgne3sg6f218msv` FOREIGN KEY (`customer_Id`) REFERENCES `customer` (`customer_Id`))
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2046)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1964)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1949)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:147)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
... 37 more

最新文章

  1. [Unity3d]向量的过度方法以及拖尾效果
  2. ios中autolayout
  3. Outer Join Query Over Dblink Can Fail With ORA-904 (Doc ID 730256.1)
  4. Leetcode#150 Evaluate Reverse Polish Notation
  5. Linux学习笔记2:如何快速的学习使用一个命令
  6. java的运行机制(基础)
  7. 关于yaml语言
  8. scrapy学习
  9. phpcms2008远程代码执行漏洞
  10. Python(五)模块
  11. freeswitch配置功能二
  12. 洛谷P5072 [Ynoi2015]盼君勿忘 [莫队]
  13. jquery 无缝轮播
  14. ABAP开发需要养成的习惯—变量定义
  15. Asp.Net Core WebAPI入门整理(二)简单示例
  16. arcgis10.1安装出现1606错误怎么办?找不到盘符
  17. Android之旅-Intent与Intent Filter[上]
  18. centos7配置kerberos服务,并使用JAAS登录
  19. Hibernate3.x异常No row with the given identifier exists 解决方法
  20. Hbase Rowkey设计

热门文章

  1. windows之cmd常用命令
  2. ECS运维:操作系统有异常?诊断日志来帮忙!
  3. LDD3 第7章 Time,Delays and Deferred Work
  4. python-zx笔记3-函数
  5. [codeforces 508E]Maximum Matching
  6. 微博API的申请
  7. Python 进阶_迭代器 & 列表解析
  8. Python做简单的字符串匹配详解
  9. 面试题22:链表中倒数第k个节点
  10. Selenium:八种元素定位方法