Hibernate annotations Map Three main ways to combine primary keys (reprint)

Source: Internet
Author: User

Hibernate annotations Map Three main ways to combine primary keys (reprint)

Transferred from: http://blog.csdn.net/robinpipi/article/details/7655388

There are three main ways of using Hibernate annotations for federated primary keys:
First, the field of the Federated primary key is placed in a single class, which needs to implement the Java.io.Serializable interface and override Equals and Hascode, then annotate the class as @embeddable, and finally in the main class (the class does not contain fields from the Federated primary Key Class) Save a reference to the Federated primary key class and generate the set and get methods, and annotate the reference as @id

Second, the field of the Federated primary key is placed in a single class, which needs to implement the Java.io.Serializable interface and override Equals and Hascode, and finally save a reference to the Federated primary key class in the main class (the class does not contain a field in the Federated primary Key Class). and generate the set and get methods, and annotate the reference as @embeddedid

Third, place the fields of the Federated primary key in a single class, which requires implementing the Java.io.Serializable interface and overriding Equals and hashcode. Finally, in the main class, which contains the fields in the Federated primary Key class, the Federated primary key field is annotated as @id. And above this class will be such annotations: @IdClass (Union primary key class. Class)

Transferred from: http://blog.sina.com.cn/s/blog_8297f0d00101012c.html

How to use:

1. Model class:

  1. @Entity
  2. @Table (name="JLEE01")
  3. Public class Jlee01 implements serializable{
  4. private Static final long serialversionuid = 3524215936351012384L;
  5. private String address;
  6. private int age;
  7. private String Email;
  8. private String phone;
  9. @Id
  10. private JleeKey01 Jleekey;

Primary KEY class: Jleekey01.java

  1. @Embeddable
  2. Public class JleeKey01 implements serializable{
  3. private Static final long serialversionuid = -3304319243957837925l;
  4. private Long ID;
  5. private String name;
  6. /** 
  7. * @return The ID
  8. */
  9. public Long GetId () {
  10. return ID;
  11. }
  12. /** 
  13. * @param ID The ID to set
  14. */
  15. public void SetId (long id) {
  16. this.id = ID;
  17. }
  18. /** 
  19. * @return The name
  20. */
  21. Public String GetName () {
  22. return name;
  23. }
  24. /** 
  25. * @param name the name to set
  26. */
  27. public void SetName (String name) {
  28. this.name = name;
  29. }
  30. @Override
  31. public Boolean equals (Object o) {
  32. if (o instanceof JleeKey01) {
  33. JleeKey01 key = (JLEEKEY01) o;
  34. if (this.id = = Key.getid () && this.name.equals (Key.getname ())) {
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. @Override
  41. public int hashcode () {
  42. return This.name.hashCode ();
  43. }
  44. }

2. Model class:

    1. @Entity
    2. @Table (name="JLEE02")
    3. Public class Jlee02 {
    4. private String address;
    5. private int age;
    6. private String Email;
    7. private String phone;
    8. @EmbeddedId
    9. private JleeKey02 Jleekey;

Primary KEY class: Jleekey02.java

Common Java class.

3. Model class:

    1. @Entity
    2. @Table (name="JLEE03")
    3. @IdClass (JleeKey03. Class)
    4. Public class Jlee03 {
    5. @Id
    6. private long id;
    7. @Id
    8. private String name;

Primary KEY class: Jleekey03.java

Common Java class.

The primary key class needs to implement the override Equals and Hascode methods, as to whether the implementation of the implements serializable interface, tested, seemingly can not be needed.

Transferred from: http://laodaobazi.iteye.com/blog/903236

Front End If you use Easyui,

Need to write this:

{field: ' joinkey.statistic_id ', title: ' Number ', Width:80,sortable:true,align: ' Center ',
Formatter:function (Value,rowdata,rowindex) {

return rowData.joinKey.statistic_id;

}},

{field: ' Joinkey.month ', title: ' Time (per month) ', Width:200,sortable:true,align: ' Center ',
Formatter:function (Value,rowdata,rowindex) {

return rowData.joinKey.month;

}}

The Joinkey is the primary key class, statistic_id, and Month are the fields of the federated key.

Hibernate annotations Map Three main ways to combine primary keys (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.