码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • JPA之使用复合主键


    文章目录

      • 1. 目标
      • 2. 数据库设计
      • 3. Entity 定义
      • 4. 使用
      • 5. 源码

    1. 目标

    在设计数据库表的时候,有时候不想定义没有意义的Id作为主键,就可以使用复合主键。这个时候使用JPA进行操作,应该如何使用复合主键?这里使用@IdClass。

    2. 数据库设计

    create table if not exists watch(
        brand varchar (20),
        name varchar (20),
        color varchar (20),
        stock int ,
        primary key (brand, name, color)
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3. Entity 定义

    import org.apache.commons.lang3.StringUtils
    import java.io.Serializable
    import javax.persistence.*
    
    
    @Entity
    @Table(name = "watch")
    @IdClass(WatchIdClass::class)
    data class WatchEntity(
        @Id
        @Column(name = "brand")
        val brand: String,
        @Id
        @Column(name = "name")
        val name: String,
        @Id
        @Column(name = "color")
        val color: String,
        @Column(name = "stock")
        val stock: Int
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    这里了使用了@IdClass注解,可以看到该注解的注释:

    Specifies a composite primary key class that is mapped to multiple fields or properties of the entity.
    The names of the fields or properties in the primary key class and the primary key fields or properties of the entity must correspond and their types must be the same.

    因此这里定义Id class

    data class WatchIdClass(
        var brand: String = StringUtils.EMPTY,
        var name: String =  StringUtils.EMPTY,
        var color: String =  StringUtils.EMPTY
    ) : Serializable
    
    • 1
    • 2
    • 3
    • 4
    • 5

    这个IdClass需要实现Serializable接口。
    另外,由于使用的是koltin,需要这个类有默认的无参构造函数,因此加了默认值,这样会生成默认构造函数。

    4. 使用

    有了上面的定义之后,repository接口就可以正常使用了。这里举了个例子
    Repository定义:

    interface WatchRepository : JpaRepository<WatchEntity, String> {
        fun findByBrandAndNameAndColor(brand: String, name: String, color: String): WatchEntity
    }
    
    • 1
    • 2
    • 3

    Service:

    @Service
    class WatchService @Autowired constructor(private val watchRepository: WatchRepository) {
    
        fun getWatch(brand: String, name: String, color: String): WatchEntity {
            return watchRepository.findByBrandAndNameAndColor(brand, name, color)
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5. 源码

    上述示例的代码在这里

  • 相关阅读:
    MindSpore:对image作normalize的目的是什么?
    linux学习(3)—— linux系统的常用命令
    IntelliJ IDEA、.NET 工具变贵,JetBrains 宣布全家桶涨价!
    ThreadLocal源码解析以及常见面试题
    Vue和SpringBoot打造中学生家校互联系统
    1028 人口普查(JAVA)
    快来看看啊,可靠的Java面经(三)
    算法通关村第十六关黄金挑战——求滑动窗口中的最大值(滑动窗口与堆方法、双端队列法和直接比较法)
    SSH框架重构SpringCloud +vue + elementUI,起码读懂代码和前端的原生JS
    torch车牌字符识别-数据集搭建(四)
  • 原文地址:https://blog.csdn.net/Apple_wolf/article/details/126207812
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号