- 浏览: 329528 次
- 性别:
- 来自: 北京
-
文章分类
- 全部博客 (211)
- Java IO (3)
- Struts (13)
- Hibernate (19)
- Spring (9)
- jQuery (2)
- Extjs (8)
- Flex (1)
- Oracle (4)
- ibatis (3)
- SQL (0)
- WorkFlow (0)
- Java Core (14)
- Ant&Maven (18)
- Java Thread (0)
- Java Socket (1)
- Java OO (2)
- Javascript (14)
- CSS&Div (2)
- MYSQL (3)
- MSSQL (0)
- Design Pattern (3)
- Data Structure&Algorithm (1)
- Source Analysis (0)
- OSGi (3)
- Linux (7)
- Network (1)
- 百无聊赖 (9)
- English (5)
- Japaness (0)
- French (0)
- Webservice (3)
- 考试认证 (0)
- C/C++ (1)
- 小题目 (1)
- Server (1)
- JSP&Servlet (18)
- JDBC (8)
- JMS (3)
- JNDI (0)
- 软件工程 (2)
- 项目管理 (2)
- UML (0)
- 文档翻译 (0)
- 架构之美 (1)
- EJB (0)
- DataBase Design (1)
- DataBase (1)
- Framework Integration (2)
- JPA (2)
- Daily Reading (8)
- 通用组件设计 (3)
- Spring DM (1)
- Spring MVC (0)
- Spring Security (0)
- 时间管理 (0)
- 成本管理 (1)
- 进度管理 (0)
- 质量管理 (0)
- 范围管理 (0)
- 环境配置 (5)
- 敏捷开发 (0)
- DB2 (2)
- 持续集成 (1)
- JVM性能调优 (2)
- Weblogic (0)
- PHP (1)
- Websphere (2)
最新评论
-
di1984HIT:
写的很好,谢谢。。
【转载】【权限控制】角色访问动态生成用户权限菜单树 -
paladin1988:
wangxuehui 写道我昨天也参加视频面试,视频面试2个人 ...
阿里巴巴面试感想 -
wangxuehui:
我昨天也参加视频面试,视频面试2个人,最后都听我说完啦,最后我 ...
阿里巴巴面试感想 -
paladin1988:
liwei113114 写道请问一下,你们最后是怎么解决tld ...
OSGi动态拔插,动态部署,动态权限菜单 -
liwei113114:
请问一下,你们最后是怎么解决tld那个问题的?
我现在也遇到这 ...
OSGi动态拔插,动态部署,动态权限菜单
其实一对多和多对一是一样的,只是看问题的角度不同。
需要的注解标签请参考前面的文章,那些标签是我从前面项目总结下来的
SQL脚本:
-- MySQL dump 10.13 Distrib 5.1.55, for Win32 (ia32) -- -- Host: localhost Database: hibernate_demo -- ------------------------------------------------------ -- Server version 5.1.55-community /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `category` -- DROP TABLE IF EXISTS `category`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `category` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `description` varchar(30) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf-8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `category` -- LOCK TABLES `category` WRITE; /*!40000 ALTER TABLE `category` DISABLE KEYS */; INSERT INTO `category` VALUES (1,'fruit','fruit category'); /*!40000 ALTER TABLE `category` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `product` -- DROP TABLE IF EXISTS `product`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `product` ( `id` int(11) NOT NULL, `name` varchar(30) NOT NULL, `price` int(11) NOT NULL, `description` varchar(30) NOT NULL, `category_id` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `product_fk` (`category_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `product` -- LOCK TABLES `product` WRITE; /*!40000 ALTER TABLE `product` DISABLE KEYS */; INSERT INTO `product` VALUES (1,'apple',10,'apple',1); /*!40000 ALTER TABLE `product` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2012-08-08 22:34:53
//Category.java
package com.zyp.examples; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; /** * Category entity. @author MyEclipse Persistence Tools */ @Entity @Table(name="category") public class Category implements java.io.Serializable { // Fields private static final long serialVersionUID = 3763960444574701564L; @Id @GenericGenerator(name="incrementGenerator", strategy="increment") @GeneratedValue(generator="incrementGenerator", strategy=GenerationType.IDENTITY) @Column(name="id") private Integer id; @Column(name="name",nullable=false, insertable=true) private String name; @Column(name="description", nullable=false, insertable=true) private String description; // Constructors /** default constructor */ public Category() { } /** full constructor */ public Category(String name, String description) { this.name = name; this.description = description; } // Property accessors 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; } public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } }
//Product.java
package com.zyp.examples; 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.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; /** * Product entity. @author MyEclipse Persistence Tools */ @Entity @Table(name="product") public class Product implements java.io.Serializable { // Fields private static final long serialVersionUID = -485648626582047874L; @Id @GenericGenerator(name="incrementGenerator", strategy="increment") @GeneratedValue(generator="incrementGenerator", strategy=GenerationType.IDENTITY) @Column(name="id") private Integer id; @Column(name="name") private String name; @Column(name="price") private Integer price; @Column(name="description") private String description; @ManyToOne(targetEntity=Category.class, fetch=FetchType.LAZY, cascade={CascadeType.ALL}) @JoinColumn(name="category_id") private Category category; // Constructors /** default constructor */ public Product() { } // Property accessors 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; } public Integer getPrice() { return this.price; } public void setPrice(Integer price) { this.price = price; } public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } }
//HibernateUti.java
package com.zyp.examples; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sessionFactory; static { try { sessionFactory = new AnnotationConfiguration().configure() .buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void shutdown() { getSessionFactory().close(); } }
//Hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost:3306/hibernate_demo </property> <property name="connection.username">root</property> <property name="connection.password">123</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="myeclipse.connection.profile">mysql</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping class="com.zyp.examples.Category" /> <mapping class="com.zyp.examples.Product" /> </session-factory> </hibernate-configuration>
经过这个例子,我也发现主键生成方式参数的设定了,呵呵。。
@Id @GenericGenerator(name="incrementGenerator(可以随便取一个名字)", strategy="increment(主键生成策略)") @GeneratedValue(generator="incrementGenerator(和上面的保持一致)", strategy=GenerationType.IDENTITY(根据需要从注解中选一个值)) @Column(name="id") private Integer id;
- Hibernate_annotation_nto1_single.rar (5.7 MB)
- 下载次数: 12
发表评论
-
Hibernate非空属性保存异常
2013-05-21 13:24 1249错误描述: Caused by: org.hi ... -
Hibernate读取错误
2013-05-21 13:22 805错误如下: Caused by: com.da ... -
【转】The user must supply a JDBC connection
2012-08-27 01:12 3361The user must supply a JDBC con ... -
Hibernate Annotation小记
2012-08-22 23:43 907老实说,Hibernate Annotation用起来 ... -
Hibernate Annotation 基于外键的一对多双向关联
2012-08-22 23:44 4187纠结了好久呀,因为没有set,关联关系没有保存对,导致插入数据 ... -
【转】Hibernate中的"Repeated column in mapping for entity"异常
2012-08-21 10:31 983文章来源:http://www.iteye.com/t ... -
Hibernate Annotation 唯一外键一对一双向关联
2012-08-20 22:38 1087继续上面的东西,修改下数据库脚本,把共享主键中的Pro ... -
【转】attempted to assign id from null one-to-one
2012-08-20 21:59 842one-to-one在hibernate中可以用来作为两张 ... -
Hibernate Annotation 共享主键一对一双向关联
2012-08-20 21:59 5978写了这么几篇都是单向的关联,干脆后面的都写双向关联吧, ... -
Hibernate Annotation 基于连接表的单向一对多关联
2012-08-17 00:29 2265趁着今晚把一对多一起搞了吧,呵呵。。 一对多的关 ... -
Hibernate的一对多关联方式
2012-08-17 00:29 853之前阅读Hibernate reference的时候看 ... -
Hibernate Annotation 基于外键的单向一对多关联
2012-08-17 00:30 1549基于外键关联的单向一对多关联是一种很少见的情况,并不推荐使用。 ... -
Hibernate Annotation入门
2012-08-15 19:40 1611废话不多说,直接上例子(附件) 数据库脚本: ... -
Hibernate 1+N问题的原理与解决
2012-08-13 20:46 1251什么时候会遇到1+N ... -
【转】Hibernate获取记录总数
2012-08-13 20:41 1148hql获取记录条数 Str ... -
Hibernate基于连接表的一对多单向关联
2012-08-09 23:13 1463基于连接表,顾名思义就是需要建立中间表来保存两个表的关联关系。 ... -
Hibernate基于外键的一对多单向关联
2012-08-09 21:42 2019一对多,一个分类Category下有多个Product,从Ca ... -
Hibernate使用注解
2012-08-04 01:37 1073//类标志 @Entity @Table(name = & ...
相关推荐
唯一外键是指在一对一关联中,一方的主键同时也作为另一方的外键,确保两个实体共享同一个ID。这可以通过在没有`@JoinColumn`的情况下让两个实体共享相同的主键生成策略来实现。例如,使用`GenerationType.IDENTITY`...
在单向一对一关联中,只有一个实体知道另一个实体的存在,而另一个实体并不知情。这里我们讨论的是单向外键关联,即一方实体持有了另一方的外键。 在Hibernate中,一对一关联可以通过`@OneToOne`注解来实现。这个...
本篇文章将详细讲解如何利用Hibernate的注解实现基于连接表的单向一对多关联。 首先,理解一对多关联:在数据库设计中,一对多关联意味着一个实体(表)可以与多个其他实体(表)相对应。例如,一个学生可以有多个...
// 单向一对一关联 @OneToOne(mappedBy = "person") private IdentityCard identityCard; // getters and setters } @Entity public class IdentityCard { @Id @GeneratedValue(strategy = GenerationType....
本篇将详细讲解如何使用Hibernate实现多对多单向关联,包括注解(Annotation)和XML配置方式。 一、多对多关联的基本概念 多对多关联意味着一个实体可以与多个其他实体关联,反之亦然。例如,学生和课程的关系,一...
在本教程中,我们将探讨如何使用注解和XML配置实现Hibernate的一对多单向关联。 首先,让我们理解一对多关联的概念。在数据库中,一对多关联意味着在一个表(父表)中的一个记录可以对应另一个表(子表)中的多个...
在Java的持久化框架Hibernate中,多对一的单向关联是一种常见的关系映射方式,它主要用于处理数据库中两个实体间的一种关联关系。在这种关系中,一个实体(多个实例)可以与另一个实体(单个实例)相关联。本文将...
以上是一对一关联的三种形式,下面介绍多对一关联。 多对一(Many-to-One) 使用@ManyToOne批注来实现多对一关联。 @ManyToOne批注有一个名为targetEntity的参数,该参数定义了目标实体名,通常不需要定义该参数...
这里,Person和Address类共享同一主键`PERSON_ID`,表明它们一对一关联。 2. **单方外键关联(Unidirectional Foreign Key Join)** 在单方外键关联中,一个实体通过外键字段引用另一个实体。配置如下: ```xml ...
根据给定文件的信息,本文将详细介绍...以上内容仅覆盖了一对一的几种常见关联方式及其配置方法,接下来还可以进一步探讨其他类型的关系映射,包括一对多、多对多等,以及这些关系在Hibernate中的具体实现方式。
-- 多对一关联 --> ``` 在`<many-to-one>`标签中,`name`属性指定了关联的属性名,`column`属性指定了数据库表中的外键列名,`class`属性指定了关联的实体类全限定名。 ##### 2. 双向关联 双向关联则指两个实体...
7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...
- **一对一关联映射** - **唯一外键关联-单向**:一个对象有一个唯一的外键指向另一个对象。 - **唯一外键关联-双向**:两个对象互相引用对方。 - **主键关联-单向**:一个对象的主键作为另一个对象的外键。 - *...
8.4.1. 一对多(one to many) / 多对一(many to one) 8.4.2. 一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many ...