zoukankan      html  css  js  c++  java
  • JPA 系列教程19-jpa-uuid主键生成策略

    ddl语句

    CREATE TABLE `t_user` (
      `id` varchar(32) NOT NULL,
      `name` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    User

    package com.jege.jpa.primary;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    import org.hibernate.annotations.GenericGenerator;
    
    /**
     * @author JE哥
     * @email 1272434821@qq.com
     * @description:jpa-uuid主键生成策略
     */
    @Entity
    @Table(name = "t_user")
    @GenericGenerator(name = "jpa-uuid", strategy = "uuid")
    public class User {
      @Id
      @GeneratedValue(generator = "jpa-uuid")
      @Column(length = 32)
      private String id;
      private String name;
    
      public String getId() {
        return id;
      }
    
      public void setId(String id) {
        this.id = id;
      }
    
      public String getName() {
        return name;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    
      @Override
      public String toString() {
        return "User [id=" + id + ", name=" + name + "]";
      }
    
    }
    

    MainTest

    package com.jege.jpa.primary;
    
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
    
    import org.junit.After;
    import org.junit.AfterClass;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    /**
     * @author JE哥
     * @entityManagerail 1272434821@qq.com
     * @description:jpa-uuid主键生成测试
     */
    public class MainTest {
      private static EntityManagerFactory entityManagerFactory = null;
      private EntityManager entityManager = null;
    
      @BeforeClass
      public static void setUpBeforeClass() throws Exception {
        entityManagerFactory = Persistence.createEntityManagerFactory("com.jege.jpa");
      }
    
      @Before
      public void setUp() throws Exception {
        entityManager = entityManagerFactory.createEntityManager();
      }
    
      @Test
      public void persist() throws Exception {
        User user = new User();
        user.setName("jege");
    
        entityManager.getTransaction().begin();
        entityManager.persist(user);
        entityManager.getTransaction().commit();
      }
    
      @After
      public void tearDown() throws Exception {
        if (entityManager != null && entityManager.isOpen()) {
          entityManager.close();
        }
      }
    
      @AfterClass
      public static void tearDownAfterClass() throws Exception {
        if (entityManagerFactory != null && entityManagerFactory.isOpen()) {
          entityManagerFactory.close();
        }
      }
    }
    

    源码地址

    https://github.com/je-ge/jpa

    如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。您的支持将鼓励我继续创作!谢谢!
    微信打赏
    支付宝打赏

  • 相关阅读:
    JS —— 数组与字符串方法
    CSS —— 选择器
    HTML —— 小记
    Javascript学习记录——数组去重
    尝鲜 vue3.x 新特性
    Weex项目快速打包
    揭秘C# SQLite的从安装到使用
    Javascript中Promise的简单使用
    网页背景图片的淡入淡出效果
    部分标点符号和数学符号的英文名字
  • 原文地址:https://www.cnblogs.com/je-ge/p/6246976.html
Copyright © 2011-2022 走看看