1.ORM:
Object Relational Mapping
对象/关系映射
2.一对一关系映射 @OneToOne
2.1.一对一单向映射
单向:A到B,不需要B到A
如一个人对应一部家庭电话
而一部家庭电话并不可能只对应一个人
实际情况下:是从人定位到电话而不是从电话定位到所有人
Person_OneToOneSingle
package orm.onetoone.single; import java.io.Serializable; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; @Entity public class Person_OneToOneSingle implements Serializable { private static final long serialVersionUID = 1L; private long id; private String name; private String sex; private int age; private HomePhone_OneToOneSingle homePhone; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId() { return id; } public String getName() { return name; } public String getSex() { return sex; } public int getAge() { return age; } @OneToOne(cascade=CascadeType.ALL,optional=true) public HomePhone_OneToOneSingle getHomePhone() { return homePhone; } public void setId(long id) { this.id = id; } public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public void setAge(int age) { this.age = age; } public void setHomePhone(HomePhone_OneToOneSingle homePhone) { this.homePhone = homePhone; } }
HomePhone_OneToOneSingle
package orm.onetoone.single; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class HomePhone_OneToOneSingle implements Serializable { private static final long serialVersionUID = 1L; private long phone; private String country,province,city,street; private int postcode; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getPhone() { return phone; } public String getCountry() { return country; } public String getProvince() { return province; } public String getCity() { return city; } public String getStreet() { return street; } public int getPostcode() { return postcode; } public void setPhone(long phone) { this.phone = phone; } public void setCountry(String country) { this.country = country; } public void setProvince(String province) { this.province = province; } public void setCity(String city) { this.city = city; } public void setStreet(String street) { this.street = street; } public void setPostcode(int postcode) { this.postcode = postcode; } }
表结构:
2.2一对一双向映射
人和身份证号:从人可以定位到身份证号,从身份证号可以定位到人是双向的一对一映射
Person_OneToOneEach
package orm.onetoone.each; import java.io.Serializable; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToOne; import orm.onetoone.single.HomePhone_OneToOneSingle; @Entity public class Person_OneToOneEach implements Serializable { private static final long serialVersionUID = 1L; private long id; private String name; private String sex; private int age; private HomePhone_OneToOneSingle homePhone; private IDCard_OneToOneEach idCard; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId() { return id; } public String getName() { return name; } public String getSex() { return sex; } public int getAge() { return age; } @OneToOne(cascade=CascadeType.ALL,optional=true) public HomePhone_OneToOneSingle getHomePhone() { return homePhone; } @OneToOne(cascade=CascadeType.ALL,optional=true) public IDCard_OneToOneEach getIdCard() { return idCard; } public void setId(long id) { this.id = id; } public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public void setAge(int age) { this.age = age; } public void setHomePhone(HomePhone_OneToOneSingle homePhone) { this.homePhone = homePhone; } public void setIdCard(IDCard_OneToOneEach idCard) { this.idCard = idCard; } }
IDCard_OneToOneEach
package orm.onetoone.each; import java.io.Serializable; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class IDCard_OneToOneEach implements Serializable{ private static final long serialVersionUID = 1L; private long id; private Person_OneToOneEach person; private String cardNumber; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId() { return id; } @OneToOne(cascade=CascadeType.ALL,mappedBy="idCard",optional=false) public Person_OneToOneEach getPerson() { return person; } public String getCardNumber() { return cardNumber; } public void setId(long id) { this.id = id; } public void setPerson(Person_OneToOneEach person) { this.person = person; } public void setCardNumber(String cardNumber) { this.cardNumber = cardNumber; } }
表结构:
3。 一对多 one-to-many
3.1一对多单向映射
人与电话之间的关系,一个人可以有多个电话,但是多个电话不一定属于一个人的,因为电话号码今天可能是你的,明天可能就是别人的了
Person_OneToManySingle
package orm.onetomany.single; import java.io.Serializable; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import orm.onetoone.each.IDCard_OneToOneEach; import orm.onetoone.single.HomePhone_OneToOneSingle; @Entity public class Person_OneToManySingle implements Serializable { private static final long serialVersionUID = 1L; private long id; private String name; private String sex; private int age; private HomePhone_OneToOneSingle homePhone; private IDCard_OneToOneEach idCard; private List<Phone_OneToManyEach> phones; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId() { return id; } @OneToMany(cascade=CascadeType.ALL) @JoinColumn(name="personID") public List<Phone_OneToManyEach> getPhones() { return phones; } @OneToOne(cascade=CascadeType.ALL,optional=true) public HomePhone_OneToOneSingle getHomePhone() { return homePhone; } @OneToOne(cascade=CascadeType.ALL,optional=true) public IDCard_OneToOneEach getIdCard() { return idCard; } public String getName() { return name; } public String getSex() { return sex; } public int getAge() { return age; } public void setId(long id) { this.id = id; } public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public void setAge(int age) { this.age = age; } public void setHomePhone(HomePhone_OneToOneSingle homePhone) { this.homePhone = homePhone; } public void setIdCard(IDCard_OneToOneEach idCard) { this.idCard = idCard; } public void setPhones(List<Phone_OneToManyEach> phones) { this.phones = phones; } }
Phone_OneToManySingle
package orm.onetomany.single; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Phone_OneToManySingle implements Serializable { private static final long serialVersionUID = 1L; private long id; private String phoneNumber; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId() { return id; } public String getPhoneNumber() { return phoneNumber; } public void setId(long id) { this.id = id; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } }
表结构:
3.2一对多双向映射
人下订单,一个人可以下多个订单,每个订单都有对应的下订单的人,他们是双向映射
Person_OneToManyEach
package orm.onetomany.each; import java.io.Serializable; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import orm.onetomany.single.Phone_OneToManySingle; import orm.onetoone.each.IDCard_OneToOneEach; import orm.onetoone.single.HomePhone_OneToOneSingle; @Entity public class Person_OneToManyEach implements Serializable { private static final long serialVersionUID = 1L; private long id; private String name; private String sex; private int age; private HomePhone_OneToOneSingle homePhone; private IDCard_OneToOneEach idCard; private List<Phone_OneToManySingle> phones; private List<Order_OneToManyEach> orders; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId() { return id; } @OneToMany(cascade=CascadeType.ALL) @JoinColumn(name="personID") public List<Phone_OneToManySingle> getPhones() { return phones; } @OneToOne(cascade=CascadeType.ALL,optional=true) public HomePhone_OneToOneSingle getHomePhone() { return homePhone; } @OneToOne(cascade=CascadeType.ALL,optional=true) public IDCard_OneToOneEach getIdCard() { return idCard; } @OneToMany(cascade=CascadeType.ALL,mappedBy="person") public List<Order_OneToManyEach> getOrders() { return orders; } public String getName() { return name; } public String getSex() { return sex; } public int getAge() { return age; } public void setId(long id) { this.id = id; } public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public void setAge(int age) { this.age = age; } public void setHomePhone(HomePhone_OneToOneSingle homePhone) { this.homePhone = homePhone; } public void setIdCard(IDCard_OneToOneEach idCard) { this.idCard = idCard; } public void setPhones(List<Phone_OneToManySingle> phones) { this.phones = phones; } public void setOrders(List<Order_OneToManyEach> orders) { this.orders = orders; } }
Order_OneToManyEach
package orm.onetomany.each; import java.io.Serializable; import java.util.Date; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class Order_OneToManyEach implements Serializable{ private static final long serialVersionUID = 1L; private long id; private Person_OneToManyEach person; private String orderNumber; private String orderName; private Date date; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId() { return id; } @ManyToOne(cascade=CascadeType.ALL,optional=false) @JoinColumn(name="personID") public Person_OneToManyEach getPerson() { return person; } public String getOrderNumber() { return orderNumber; } public String getOrderName() { return orderName; } public Date getDate() { return date; } public void setId(long id) { this.id = id; } public void setPerson(Person_OneToManyEach person) { this.person = person; } public void setOrderNumber(String orderNumber) { this.orderNumber = orderNumber; } public void setOrderName(String orderName) { this.orderName = orderName; } public void setDate(Date date) { this.date = date; } }
表结构
4.多对一单向关联
多的一方知道自己属于一的一方,而一的一方并不知道多的一方,如人和国家,很多人肯定属于一个国家,而国家并不知道每个人
Person_ManyToOneSingle
package orm.manytoone.single; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class Person_ManyToOneSingle implements Serializable { private static final long serialVersionUID = 1L; private long id; private String name; private String sex; private int age; private Country_ManyToOneSingle country; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId() { return id; } @ManyToOne @JoinColumn(name="countryID") public Country_ManyToOneSingle getCountry() { return country; } public String getName() { return name; } public String getSex() { return sex; } public int getAge() { return age; } public void setId(long id) { this.id = id; } public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public void setAge(int age) { this.age = age; } public void setCountry(Country_ManyToOneSingle country) { this.country = country; } }
Country_ManyToOneSingle
package orm.manytoone.single; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Country_ManyToOneSingle implements Serializable { private static final long serialVersionUID = 1L; private long id; private String name; private String area; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId() { return id; } public String getName() { return name; } public String getArea() { return area; } public void setId(long id) { this.id = id; } public void setName(String name) { this.name = name; } public void setArea(String area) { this.area = area; } }
表结构:
5.多对多many-to-many
5.1多对多单向映射
学生和老师的关系,就是多对多关系,我们可以强制让学生知道老师的信息,而老师不用记住所有所教的学生的信息,这就可以用多对多单向关系
Student_ManyToManySingle
package orm.manytomany.single; import java.io.Serializable; import java.util.List; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Student_ManyToManySingle implements Serializable { private static final long serialVersionUID = 1L; private long id; private String name; private String sex; private List<Teacher_ManyToManySingle> teachers; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId() { return id; } public String getName() { return name; } public String getSex() { return sex; } @ManyToMany public List<Teacher_ManyToManySingle> getTeachers() { return teachers; } public void setId(long id) { this.id = id; } public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public void setTeachers(List<Teacher_ManyToManySingle> teachers) { this.teachers = teachers; } }
Teacher_ManyToManySingle
package orm.manytomany.single; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Teacher_ManyToManySingle implements Serializable { private static final long serialVersionUID = 1L; private long id; private String name; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId() { return id; } public String getName() { return name; } public void setId(long id) { this.id = id; } public void setName(String name) { this.name = name; } }
表结构
5.2多对多双向映射
如果上面的例子让老师也知道他所有学生的信息就可以用多对多双向映射关系来表示
Student_ManyToManyEach
package orm.manytomany.each; import java.io.Serializable; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; @Entity public class Student_ManyToManyEach implements Serializable { private static final long serialVersionUID = 1L; private long id; private String name; private String sex; private List<Teacher_ManyToManyEach> teachers; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId() { return id; } public String getName() { return name; } public String getSex() { return sex; } @ManyToMany(cascade=CascadeType.ALL) @JoinTable( name="StudentsAndTeachers", joinColumns={@JoinColumn(name="studentID")}, inverseJoinColumns={@JoinColumn(name="teacherID")} ) public List<Teacher_ManyToManyEach> getTeachers() { return teachers; } public void setId(long id) { this.id = id; } public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public void setTeachers(List<Teacher_ManyToManyEach> teachers) { this.teachers = teachers; } }
Teacher_ManyToManyEach
package orm.manytomany.each; import java.io.Serializable; import java.util.List; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Teacher_ManyToManyEach implements Serializable { private static final long serialVersionUID = 1L; private long id; private String name; private List<Student_ManyToManyEach> students; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getId() { return id; } @ManyToMany(mappedBy="teachers") public List<Student_ManyToManyEach> getStudents() { return students; } public String getName() { return name; } public void setId(long id) { this.id = id; } public void setName(String name) { this.name = name; } public void setStudents(List<Student_ManyToManyEach> students) { this.students = students; } }
表结构:
相关推荐
EJB实体Bean通过JPA(Java Persistence API)或Hibernate等ORM框架与数据库交互。 ### 二、Eclipse中创建和配置EJB实体Bean 1. **新建EJB项目**:在Eclipse中,选择“File” -> “New” -> “Other” -> ...
2. **src/main/java**: 这个目录包含了项目的源代码,特别是EJB实体Bean类。EJB 3实体Bean通常继承自`javax.ejb.EntityBean`接口或者使用注解(如`@Entity`)来标识。这些Bean代表数据库表的持久化对象,并且可以...
EJB Core涵盖了EJB的基础架构,包括会话bean(Session Beans)、实体bean(Entity Beans)和消息驱动bean(Message-Driven Beans)。在EJB 3.0中,主要引入了注解驱动的编程模型,简化了开发过程,减少了XML配置。...
实体Bean(Entity Beans)是EJB的一种类型,它代表了业务逻辑中的持久数据,通常对应数据库中的记录。在这个"购物车"案例中,我们将会探讨如何使用EJB来开发一个简单的实体Bean,以及与之交互的Web前端。 1. **EJB...
在EJB 3.0中,实体Bean采用了注解驱动的模式,使得无须编写复杂的接口和Home接口,大大简化了实体Bean的定义。 - **会话Bean(Session Bean)**:提供方法来执行业务逻辑,可以分为有状态(Stateful)和无状态...
在这个“EJB实体Bean学习”主题中,我们将深入探讨实体Bean的两大类型——BMP(Business Managed Persistence)和CMP(Container Managed Persistence),以及如何进行基本的CRUD(创建、读取、更新和删除)操作。...
在学习和实践中,可以通过以下步骤创建一个单表映射的EJB实体Bean: 1. 定义实体类,使用`@Entity`、`@Table`等注解。 2. 注解属性以匹配数据库表的列。 3. 创建`EntityManager`实例,用于操作Bean。 4. 使用`...
1. **实体Bean(Entity Beans)**:EJB 3.0引入了注解驱动的实体管理,使得持久化更加简单。实体Bean代表数据库中的持久化对象,不再需要编写复杂的EntityHome和EntityBean接口,而是通过@Entity和@Id注解定义实体类...
实体Bean在EJB 3.0中被重新设计为实体类(Entities),采用Java Persistence API(JPA)进行数据持久化,支持对象关系映射(ORM),从而实现了数据库操作的抽象,使得开发者无需关心底层的SQL语句,只需通过面向对象...
- **容器管理的实体Bean**:EJB 3.0支持无状态会话Bean、有状态会话Bean和消息驱动Bean,其中,实体Bean被重命名为“实体”(Entity),并引入了“管理实体”(Managed Entity)的概念,这使得EJB可以更好地处理...
- **容器管理的持久化**:EJB容器现在提供了更强大的持久化支持,包括对实体Bean的生命周期管理。 2. **Java Persistence API (JPA)**: - **实体类与持久化上下文**:在JPA中,实体类是持久化的基础单位,而持久...
**JPA学习源码(EJB实体Bean)** Java Persistence API(JPA)是Java平台上的一个标准,用于管理和持久化对象。它简化了在关系数据库中存储和检索Java对象的过程,是JAVA EE5中引入的重要组件。JPA通过提供ORM(对象...
1. **实体Bean(Entity Beans)**:在EJB 3.0中,实体Bean的定义更加简洁。不再需要编写Home和Remote接口,而是通过@Entity注解来标识一个Java类作为持久化实体。同时,@Id注解用于指定主键字段,而JPA处理了大部分...
EJB 3.0引入了新的实体bean模式,即CMP(Container-Managed Persistence)。通过使用`@Entity`和JPA(Java Persistence API),开发者可以直接在bean类中定义字段并使用`@Id`来指定主键,从而实现数据存储的透明化...
JPA是Java平台上的ORM(Object-Relational Mapping)规范,用于处理Java对象与关系数据库之间的映射。它允许开发者以面向对象的方式来操作数据库,减少了SQL的使用。JPA通过注解或XML配置来定义对象与表的映射关系,...
实体Bean是EJB中用于表示业务数据的组件,JPA的加入,使得实体Bean的定义和使用变得更加简洁高效。开发者可以通过简单的注解,如@Entity、@Table、@Id等,来描述实体类与数据库表之间的映射关系,以及主键生成策略等...
5. **实体Bean(Entity Beans)**:EJB3.0中的实体Bean主要基于JPA,使用`@Entity`注解标识,允许开发者直接操作数据库记录。实体Bean可以通过`@GeneratedValue`注解来自动分配主键。 6. **容器管理的事务...
EJB3的实体Bean通常与Java Persistence API (JPA)一起使用,JPA是Java EE的一部分,提供了一种标准的方式来管理对象-关系映射(ORM)。Hibernate是一个流行的JPA实现,提供了更丰富的功能和性能优化。 3. **注解...
**企业级JavaBeans (EJB) 2.0 实体Bean详解** EJB(Enterprise JavaBeans)是Java EE(Enterprise Edition)平台的核心组件之一,它为构建可部署在服务器端的分布式应用程序提供了一种框架。EJB 2.0是EJB规范的一个...
3. **实体Bean的JPA集成**: EJB3引入了Java Persistence API (JPA),使得ORM(对象关系映射)更加直观。 ### 开发Web服务 Web服务是一种基于开放标准(如SOAP, WSDL, UDDI)的软件接口,允许不同系统之间的交互。...