数据库mysql:user50表和order50表是一对多的关系
order50表有 外键: user_id
user类:
package com.c50.entity; import java.util.Date; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; import org.hibernate.annotations.GenericGenerator; //代表此类参与ORM映射,此注解必须要有 @Entity //代表user这个类映射了一个表user50,如果表名和类名一样,此注解可以省略 @Table(name="user50") //读写缓存权限 @Cache(usage=CacheConcurrencyStrategy.READ_WRITE) public class User { //主键,此注解必须要有 @Id //数据库表中主键列名为id,如果实体类属性名和列名相同,此注解可以省略 @Column(name="id") //主键的维护策略 @GenericGenerator(name="inc50",strategy="increment") @GeneratedValue(generator="inc50") private Integer userID; private String name; private Integer age; @Column(name="birth") //此属性是日期类型,time:只保存时间 date:只保存日期 timestamp:(默认)日期+时间 @Temporal(TemporalType.DATE) private Date birthday; //关系属性 //mappedBy="对方中映射着外键的关系属性"=="order类中关系属性为user" //延迟加载:fetch = FetchType.LAZY @OneToMany(mappedBy="user",fetch=FetchType.LAZY) //ALL:增删改都可以级联(cascade) //save-update:增加和更新可以级联 //delete:删除可以级联 //如果不设置,则增删改都不可以级联 //注意:查询例外,不受级联权限的控制,默认级联。 @Cascade(value=CascadeType.DELETE) private Set<Order> orders=new HashSet<Order>(); public Set<Order> getOrders() { return orders; } public void setOrders(Set<Order> orders) { this.orders = orders; } public User(){} public User(Integer userID, String name, Integer age, Date birthday) { super(); this.userID = userID; this.name = name; this.age = age; this.birthday = birthday; } public Integer getUserID() { return userID; } public void setUserID(Integer userID) { this.userID = userID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User [userID=" + userID + ", name=" + name + ", age=" + age + ", birthday=" + birthday + "]"; } }
order类:
package com.c50.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="order50") public class Order { @Id @GenericGenerator(name="inc50",strategy="increment") @GeneratedValue(generator="inc50") private Integer id; private Double price; private String note; //关系属性 @ManyToOne //数据库中order表,外键名为user_id,指向user表 @JoinColumn(name="user_id") private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Override public String toString() { return "Order [id=" + id + ", price=" + price + ", note=" + note + "]"; } public Order(){} public Order(Integer id, Double price, String note) { super(); this.id = id; this.price = price; this.note = note; } }
时间: 2024-10-14 14:09:12