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:
- @Entity
- @Table (name="JLEE01")
- Public class Jlee01 implements serializable{
- private Static final long serialversionuid = 3524215936351012384L;
- private String address;
- private int age;
- private String Email;
- private String phone;
- @Id
- private JleeKey01 Jleekey;
Primary KEY class: Jleekey01.java
- @Embeddable
- Public class JleeKey01 implements serializable{
- private Static final long serialversionuid = -3304319243957837925l;
- private Long ID;
- private String name;
- /**
- * @return The ID
- */
- public Long GetId () {
- return ID;
- }
- /**
- * @param ID The ID to set
- */
- public void SetId (long id) {
- this.id = ID;
- }
- /**
- * @return The name
- */
- Public String GetName () {
- return name;
- }
- /**
- * @param name the name to set
- */
- public void SetName (String name) {
- this.name = name;
- }
- @Override
- public Boolean equals (Object o) {
- if (o instanceof JleeKey01) {
- JleeKey01 key = (JLEEKEY01) o;
- if (this.id = = Key.getid () && this.name.equals (Key.getname ())) {
- return true;
- }
- }
- return false;
- }
- @Override
- public int hashcode () {
- return This.name.hashCode ();
- }
- }
2. Model class:
- @Entity
- @Table (name="JLEE02")
- Public class Jlee02 {
- private String address;
- private int age;
- private String Email;
- private String phone;
- @EmbeddedId
- private JleeKey02 Jleekey;
Primary KEY class: Jleekey02.java
Common Java class.
3. Model class:
- @Entity
- @Table (name="JLEE03")
- @IdClass (JleeKey03. Class)
- Public class Jlee03 {
- @Id
- private long id;
- @Id
- 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)