In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "Spring Data JPA how to build the joint primary key of the table", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Spring Data JPA how to build the federated primary key of the table" this article.
The first way:
The first way is to annotate @ Id directly to both fields above the class attribute, as shown below, to add a federated primary key to the stuNo and stuName fields:
@ Entity@Table (name = "student") public class Student {@ Id @ Column (name = "stu_no", nullable = false, length = 11) private Integer stuNo; @ Id @ Column (name = "stu_name", nullable = false, length = 128) private String stuName; @ Column (name = "stu_age", nullable = false, length = 3) private Integer stuAge; @ Column (name = "class_id", nullable = false, length = 8) private String classId;}
It's just important to note that entity classes need to implement the Serializable interface.
This method is not very good, although the table can be created successfully, but when using JpaRepository, you need to specify the type of primary key ID, which will report an error, so it is better to use the second method.
The second way:
It is also very simple to implement, we need to create a new class, or to establish a joint primary key with stuNo and stuName, this class needs to implement the Serializable interface.
Public class StudentUPK implements Serializable {private Integer stuNo; private String stuName;}
Then add @ IdClass annotation to the entity class Student, and add @ Id annotation to the two fields:
@ Entity@IdClass (StudentUPK.class) @ Table (name = "student") public class Student {@ Id @ Column (name = "stu_no", nullable = false, length = 11) private Integer stuNo; @ Id @ Column (name = "stu_name", nullable = false, length = 128) private String stuName; @ Column (name = "stu_age", nullable = false, length = 3) private Integer stuAge; @ Column (name = "class_id", nullable = false, length = 8) private String classId;}
In this way, you can successfully create the table, and when using JpaRepoistory, you can specify which StudentUPK class the primary key is, like this: public interface StudentRepository extends JpaRepository.
The above is all the content of the article "how to create the federated primary key of a table by Spring Data JPA". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.