Hibernate,JPA注解@OneToOne_JoinColumn

忘是亡心i 2022-03-31 15:16 387阅读 0赞

一对一(One-to-one),外键关联

用例代码如下:

  • 数据库DDL语句

1,CAT表

  1. 1 create table CAT
  2. 2 (
  3. 3 id VARCHAR2(32 CHAR) not null,
  4. 4 create_time TIMESTAMP(6),
  5. 5 update_time TIMESTAMP(6),
  6. 6 cat_name VARCHAR2(255 CHAR),
  7. 7 first_name VARCHAR2(255 CHAR),
  8. 8 last_name VARCHAR2(255 CHAR),
  9. 9 version NUMBER(10) not null
  10. 10 )

2,CAT_INFO表

  1. 1 create table CAT_INFO
  2. 2 (
  3. 3 id VARCHAR2(32 CHAR) not null,
  4. 4 create_time TIMESTAMP(6),
  5. 5 update_time TIMESTAMP(6),
  6. 6 address VARCHAR2(255 CHAR),
  7. 7 birthday TIMESTAMP(6),
  8. 8 cat_id VARCHAR2(32 CHAR)
  9. 9 )
  • hibernate.cfg.xml

    1 <?xml version=”1.0” encoding=”utf-8” ?>
    2 <!DOCTYPE hibernate-configuration
    3 PUBLIC “-//Hibernate/Hibernate Configuration DTD//EN”
    4 “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    5
    6
    7
    8 org.hibernate.dialect.Oracle10gDialect
    9 oracle.jdbc.OracleDriver
    10 jdbc:oracle:thin:@127.0.0.1:1521:orcl
    11 wxuatuser
    12 xlh
    13 true
    14
    15 update
    16
    17
    18
    19
    20
    21

    22

  • java类

实体类 - 基类

  1. 1 package model;
  2. 2 import java.io.Serializable;
  3. 3 import java.util.Date;
  4. 4 import javax.persistence.Column;
  5. 5 import javax.persistence.GeneratedValue;
  6. 6 import javax.persistence.Id;
  7. 7 import javax.persistence.MappedSuperclass;
  8. 8 import org.hibernate.annotations.GenericGenerator;
  9. 9 /**
  10. 10 * 实体类 - 基类
  11. 11 */
  12. 12 @MappedSuperclass
  13. 13 public class BaseEntity implements Serializable {
  14. 14
  15. 15 private static final long serialVersionUID = -6718838800112233445L;
  16. 16
  17. 17 private String id;// ID
  18. 18 private Date create_time;// 创建日期
  19. 19 private Date update_time;// 修改日期
  20. 20 @Id
  21. 21 @Column(length = 32, nullable = true)
  22. 22 @GeneratedValue(generator = "uuid")
  23. 23 @GenericGenerator(name = "uuid", strategy = "uuid")
  24. 24 public String getId() {
  25. 25 return id;
  26. 26 }
  27. 27 public void setId(String id) {
  28. 28 this.id = id;
  29. 29 }
  30. 30 @Column(updatable = false)
  31. 31 public Date getCreate_time() {
  32. 32 return create_time;
  33. 33 }
  34. 34 public void setCreate_time(Date create_time) {
  35. 35 this.create_time = create_time;
  36. 36 }
  37. 37 public Date getUpdate_time() {
  38. 38 return update_time;
  39. 39 }
  40. 40 public void setUpdate_time(Date update_time) {
  41. 41 this.update_time = update_time;
  42. 42 }
  43. 43 @Override
  44. 44 public int hashCode() {
  45. 45 return id == null ? System.identityHashCode(this) : id.hashCode();
  46. 46 }
  47. 47 @Override
  48. 48 public boolean equals(Object obj) {
  49. 49 if (this == obj) {
  50. 50 return true;
  51. 51 }
  52. 52 if (obj == null) {
  53. 53 return false;
  54. 54 }
  55. 55 if (getClass().getPackage() != obj.getClass().getPackage()) {
  56. 56 return false;
  57. 57 }
  58. 58 final BaseEntity other = (BaseEntity) obj;
  59. 59 if (id == null) {
  60. 60 if (other.getId() != null) {
  61. 61 return false;
  62. 62 }
  63. 63 } else if (!id.equals(other.getId())) {
  64. 64 return false;
  65. 65 }
  66. 66 return true;
  67. 67 }
  68. 68 }

实体类

Cat.java

  1. 1 package a7_OneToOne_JoinColumn;
  2. 2 import javax.persistence.AttributeOverride;
  3. 3 import javax.persistence.AttributeOverrides;
  4. 4 import javax.persistence.CascadeType;
  5. 5 import javax.persistence.Column;
  6. 6 import javax.persistence.Embedded;
  7. 7 import javax.persistence.Entity;
  8. 8 import javax.persistence.JoinColumn;
  9. 9 import javax.persistence.OneToOne;
  10. 10 import javax.persistence.Version;
  11. 11 import model.BaseEntity;
  12. 12 import org.hibernate.annotations.DynamicInsert;
  13. 13 import org.hibernate.annotations.DynamicUpdate;
  14. 14
  15. 15 @Entity
  16. 16 @DynamicInsert
  17. 17 @DynamicUpdate
  18. 18 public class Cat extends BaseEntity{
  19. 19 /**
  20. 20 * 实体类
  21. 21 */
  22. 22 private static final long serialVersionUID = -2776330321385582872L;
  23. 23
  24. 24 private Cat_Info cat_Info;
  25. 25
  26. 26 private String cat_name;
  27. 27 private Name name;
  28. 28 private int version;
  29. 29
  30. 30 @Version
  31. 31 public int getVersion() {
  32. 32 return version;
  33. 33 }
  34. 34
  35. 35 public void setVersion(int version) {
  36. 36 this.version = version;
  37. 37 }
  38. 38
  39. 39 public String getCat_name() {
  40. 40 return cat_name;
  41. 41 }
  42. 42
  43. 43 public void setCat_name(String cat_name) {
  44. 44 this.cat_name = cat_name;
  45. 45 }
  46. 46
  47. 47 @Embedded
  48. 48 @AttributeOverrides({
  49. 49 @AttributeOverride(name = "first_name", column = @Column(name = "first_name")),
  50. 50 @AttributeOverride(name = "last_name", column = @Column(name = "last_name")) })
  51. 51 public Name getName() {
  52. 52 return name;
  53. 53 }
  54. 54
  55. 55 public void setName(Name name) {
  56. 56 this.name = name;
  57. 57 }
  58. 58
  59. 59 /*
  60. 60 * mappedBy属性:
  61. 61 * 如果关系是单向的,则该关联提供程序确定拥有该关系的字段。
  62. 62 * 如果关系是双向的,则将关联相反(非拥有)方上的 mappedBy 元素设置为拥
  63. 63 * 有此关系的字段或属性的名称
  64. 64 */
  65. 65 @OneToOne(cascade = CascadeType.ALL,mappedBy="cat")
  66. 66 @JoinColumn(name="id", referencedColumnName="id", unique=true, nullable=false, updatable=true)
  67. 67 public Cat_Info getCat_Info() {
  68. 68 return cat_Info;
  69. 69 }
  70. 70
  71. 71 public void setCat_Info(Cat_Info cat_Info) {
  72. 72 this.cat_Info = cat_Info;
  73. 73 }
  74. 74 }

Cat_Info.java

  1. 1 package a7_OneToOne_JoinColumn;
  2. 2 import java.util.Date;
  3. 3 import javax.persistence.CascadeType;
  4. 4 import javax.persistence.Entity;
  5. 5 import javax.persistence.FetchType;
  6. 6 import javax.persistence.OneToOne;
  7. 7 import model.BaseEntity;
  8. 8
  9. 9 @Entity
  10. 10 public class Cat_Info extends BaseEntity{
  11. 11 /**
  12. 12 *
  13. 13 */
  14. 14 private static final long serialVersionUID = 8258082211095664606L;
  15. 15
  16. 16 private Cat cat;
  17. 17 private String address;
  18. 18 private Date birthday;
  19. 19
  20. 20 public String getAddress() {
  21. 21 return address;
  22. 22 }
  23. 23
  24. 24 public void setAddress(String address) {
  25. 25 this.address = address;
  26. 26 }
  27. 27
  28. 28 public Date getBirthday() {
  29. 29 return birthday;
  30. 30 }
  31. 31
  32. 32 public void setBirthday(Date birthday) {
  33. 33 this.birthday = birthday;
  34. 34 }
  35. 35
  36. 36
  37. 37 @OneToOne(cascade = {CascadeType.ALL},fetch = FetchType.EAGER)
  38. 38 public Cat getCat() {
  39. 39 return cat;
  40. 40 }
  41. 41
  42. 42 public void setCat(Cat cat) {
  43. 43 this.cat = cat;
  44. 44 }
  45. 45 }

组件类

  1. 1 package a7_OneToOne_JoinColumn;
  2. 2 import java.io.Serializable;
  3. 3 import javax.persistence.Embeddable;
  4. 4
  5. 5 @Embeddable
  6. 6 public class Name implements Serializable {
  7. 7 /**
  8. 8 * 嵌入式组建
  9. 9 */
  10. 10 private static final long serialVersionUID = -2776330321385582872L;
  11. 11
  12. 12 private String first_name;
  13. 13 private String last_name;
  14. 14 public String getFirst_name() {
  15. 15 return first_name;
  16. 16 }
  17. 17 public void setFirst_name(String first_name) {
  18. 18 this.first_name = first_name;
  19. 19 }
  20. 20 public String getLast_name() {
  21. 21 return last_name;
  22. 22 }
  23. 23 public void setLast_name(String last_name) {
  24. 24 this.last_name = last_name;
  25. 25 }
  26. 26 }

Dao

  1. 1 package daoUtil;
  2. 2 import org.hibernate.HibernateException;
  3. 3 import org.hibernate.Session;
  4. 4 import org.hibernate.SessionFactory;
  5. 5 import org.hibernate.Transaction;
  6. 6 import org.hibernate.cfg.Configuration;
  7. 7 import org.hibernate.service.ServiceRegistry;
  8. 8 import org.hibernate.service.ServiceRegistryBuilder;
  9. 9
  10. 10 public class HibernateUtil {
  11. 11
  12. 12 private static final SessionFactory sessionFactory;
  13. 13
  14. 14 static {
  15. 15 try {
  16. 16 Configuration cfg = new Configuration().configure();
  17. 17 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
  18. 18 .applySettings(cfg.getProperties()).buildServiceRegistry();
  19. 19 sessionFactory = cfg.buildSessionFactory(serviceRegistry);
  20. 20 } catch (Throwable ex) {
  21. 21 throw new ExceptionInInitializerError(ex);
  22. 22 }
  23. 23 }
  24. 24
  25. 25 public static Session getSession() throws HibernateException {
  26. 26 return sessionFactory.openSession();
  27. 27 }
  28. 28
  29. 29 public static Object save(Object obj){
  30. 30 Session session = HibernateUtil.getSession();
  31. 31 Transaction tx = null;
  32. 32 try {
  33. 33 tx = session.beginTransaction();
  34. 34 session.save(obj);
  35. 35 tx.commit();
  36. 36 } catch (RuntimeException e) {
  37. 37 if (tx != null) {
  38. 38 tx.rollback();
  39. 39 }
  40. 40 throw e;
  41. 41 } finally {
  42. 42 session.close();
  43. 43 }
  44. 44 return obj;
  45. 45 }
  46. 46
  47. 47 public static void delete(Class<?> clazz,String id){
  48. 48 Session session = HibernateUtil.getSession();
  49. 49 Transaction tx = null;
  50. 50 try {
  51. 51 tx = session.beginTransaction();
  52. 52 Object obj = session.get(clazz,id);
  53. 53 session.delete(obj);
  54. 54 tx.commit();
  55. 55 } catch (RuntimeException e) {
  56. 56 if (tx != null) {
  57. 57 tx.rollback();
  58. 58 }
  59. 59 throw e;
  60. 60 } finally {
  61. 61 session.close();
  62. 62 }
  63. 63 }
  64. 64 }

main

  1. 1 package a7_OneToOne_JoinColumn;
  2. 2 import java.util.Date;
  3. 3 import daoUtil.HibernateUtil;
  4. 4
  5. 5 public class Test_OneToOne_JoinColumn {
  6. 6
  7. 7 private Cat save(){
  8. 8
  9. 9 Cat_Info info = new Cat_Info();
  10. 10 info.setAddress("201503291");
  11. 11 info.setBirthday(new Date());
  12. 12
  13. 13 Cat cat = new Cat();
  14. 14 cat.setCat_name("a7_OneToOne_JoinColumn");
  15. 15 cat.setName(new Name());
  16. 16 cat.setCat_Info(info);
  17. 17 info.setCat(cat);
  18. 18
  19. 19 HibernateUtil.save(info);
  20. 20 System.out.println(cat.getId());
  21. 21 return cat;
  22. 22 }
  23. 23
  24. 24 public static void main(String[] args) {
  25. 25
  26. 26 Cat cat = new Test_OneToOne_JoinColumn().save();
  27. 27 Cat cat1 = (Cat)HibernateUtil.getSession().get(Cat.class, cat.getId());
  28. 28 System.out.println(cat1.getId());
  29. 29
  30. 30 Cat_Info cat_Info = (Cat_Info)HibernateUtil.getSession().get(Cat_Info.class, cat1.getCat_Info().getId());
  31. 31 System.out.println(cat_Info.getId());
  32. 32 }
  33. 33 }

环境:JDK1.6,MAVEN,tomcat,eclipse

源码地址:http://files.cnblogs.com/files/xiluhua/hibernate%40OneToOne\_JoinColumn.rar

发表评论

表情:
评论列表 (有 0 条评论,387人围观)

还没有评论,来说两句吧...

相关阅读

    相关 注解_

      开始学习注解时觉得注解的符号看不太习惯,加上概念上也觉得比较难得懂,后来发现这是一个很有用的东西,从新翻来看看。 注解还是很重要的在开发中,现在的开发都开始基于注解进行开