Hirbernate 一对多

实体类:

package com.zking.entity2;

import java.util.HashSet;
import java.util.Set;

public class Order {
//	create table t_hibernate_order
//	(
//	  order_id int primary key auto_increment,
//	  order_no varchar(50) not null
//	);
	private Integer orderId;
	private String orderNo;
	//一对多就是多
	//注意:变量属性一定用接口进行接受
	private Set<OrderItem> orderItems = new HashSet<OrderItem>();
	
	private Integer initOrderItems = 0;//0代表懒加载,1代表强制加载
	
	
	public Integer getInitOrderItems() {
		return initOrderItems;
	}
	public void setInitOrderItems(Integer initOrderItems) {
		this.initOrderItems = initOrderItems;
	}
	public Set<OrderItem> getOrderItems() {
		return orderItems;
	}
	public void setOrderItems(Set<OrderItem> orderItems) {
		this.orderItems = orderItems;
	}
	public Integer getOrderId() {
		return orderId;
	}
	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}
	public String getOrderNo() {
		return orderNo;
	}
	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}
	
}
package com.zking.entity2;

public class OrderItem {
//	create table t_hibernate_order_item
//	(
//	  order_item_id int primary key auto_increment,
//	  product_id int not null,
//	  quantity int not null,
//	  oid int not null,
//	  foreign key(oid) references t_hibernate_order(order_id)
//	);
	private Integer orderItemId;
	private Integer productId;
	private Integer quantity;
	private Integer oid;
	
	private Order order;
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}
	public Integer getOrderItemId() {
		return orderItemId;
	}
	public void setOrderItemId(Integer orderItemId) {
		this.orderItemId = orderItemId;
	}
	public Integer getProductId() {
		return productId;
	}
	public void setProductId(Integer productId) {
		this.productId = productId;
	}
	public Integer getQuantity() {
		return quantity;
	}
	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}
	public Integer getOid() {
		return oid;
	}
	public void setOid(Integer oid) {
		this.oid = oid;
	}
	
}

配置文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-11-25 19:59:59 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.zking.entity2.Order" table="t_hibernate_order">
        <id name="orderId" type="java.lang.Integer">
            <column name="order_id" />
            <generator class="increment" />
        </id>
        <property name="orderNo" type="java.lang.String">
            <column name="order_no" />
        </property>
        <set name="orderItems" table="ORDERITEM" inverse="false" lazy="true" cascade="all">
            <key>
                <column name="oid" />
            </key>
            <one-to-many class="com.zking.entity2.OrderItem" />
        </set>
    </class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-11-25 19:59:59 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.zking.entity2.OrderItem" table="t_hibernate_order_item">
        <id name="orderItemId" type="java.lang.Integer">
            <column name="order_item_id" />
            <generator class="increment" />
        </id>
        <property name="productId" type="java.lang.Integer">
            <column name="product_id" />
        </property>
        <property name="quantity" type="java.lang.Integer">
            <column name="quantity" />
        </property>
        <property name="oid" type="java.lang.Integer">
            <column name="oid" />
        </property>
        <many-to-one name="order" class="com.zking.entity2.Order" cascade="all" insert="false" update="false">
            <column name="oid" />
        </many-to-one>
    </class>
</hibernate-mapping>

测试:

  
      @Test
      public void oneToMony() {
    	  SessionFactory factory = new Configuration().configure().buildSessionFactory();
          Session session = factory.openSession();
          session.beginTransaction();
          
  		  Order o = session.get(Order.class, 1);
  		  Hibernate.initialize(o.getOrderItems());
  		  System.out.println(o.getOrderNo());
  		  System.out.println(o.getOrderItems().size());
          
          
          
          session.getTransaction().commit();
      }

 

在Hibernate中创建课程表实体类时,可以考虑使用以下步骤: 1. 创建实体类Course,表示课程表的条目。 ```java @Entity @Table(name = "course") public class Course { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; // other attributes @OneToMany(mappedBy = "course") private List<We> weList; // getters and setters } ``` 2. 创建实体类We,表示we表中的多条数据。 ```java @Entity @Table(name = "we") public class We { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String data; // other attributes @ManyToOne @JoinColumn(name = "course_id") private Course course; // getters and setters } ``` 在Course实体类中使用@OneToMany注解,表示一个课程可能对应多个we数据,通过mappedBy属性指定了在We实体类中与Course的映射关系。 增删改查方法的编写可以参考以下示例: - 增加课程: ```java public void addCourse(Course course) { Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); session.save(course); transaction.commit(); session.close(); } ``` - 删除课程: ```java public void deleteCourse(Long courseId) { Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Course course = session.get(Course.class, courseId); if (course != null) { session.delete(course); } transaction.commit(); session.close(); } ``` - 修改课程: ```java public void updateCourse(Course course) { Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); session.update(course); transaction.commit(); session.close(); } ``` - 查询课程: ```java public Course getCourse(Long courseId) { Session session = sessionFactory.openSession(); Course course = session.get(Course.class, courseId); session.close(); return course; } ``` 以上是一个简单的示例,你可以根据具体需求进行适当修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值