Hibernate No primary key table (composite primary key) mapping

Source: Internet
Author: User

1. Why do I have a composite primary key mapping
In reality we may encounter many tables that may not have a primary key, so what would be the result of using it after mapping? Is it normal to get what we want? The result should not get the desired result, and the resulting may be the following error:

caused by:org.hibernate.AnnotationException:No identifier specified for entity:xxx.xxx.xxx

The result tells us that the Hibernate mapping table requires a primary key.

Therefore, the composite primary key mapping is born.

2. Attention points
The composite primary key is represented as a primary key using an embeddable class, so you need to use the @id and @embeddable two annotations. Another way is to use the @embeddedid annotation. There is also one way to do that--using @idclass annotations. For details, see "Hibernate-annotations-3.4.0.ga 2.2.6. Map Composite primary and foreign keys".

Of course, you just have to choose one of them. ^_^

Note that the classes you rely on must implement serializable and implement the Equals ()/hashcode () method.

Give a specific example:

2.1. User.java
[Java]
<span style= "FONT-SIZE:12PX;" >package Com.sourcefour.bean;
Default Package
Import Javax.persistence.AttributeOverride;
Import Javax.persistence.AttributeOverrides;
Import Javax.persistence.Column;
Import Javax.persistence.EmbeddedId;
Import javax.persistence.Entity;
Import javax.persistence.Table;
/**
* User entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table (name= "T_USER_COMPOSITE_PK"
)
public class User implements Java.io.Serializable {
Fields
Private UserId ID;
Constructors
/** default Constructor */
Public User () {
}
/** Full Constructor */
Public User (UserId ID) {
This.id = ID;
}
Property accessors
@EmbeddedId
@AttributeOverrides ({
@AttributeOverride (name= "IntId",[email protected](name= "IntId", Nullable=false)),
@AttributeOverride (name= "Varcname",[email protected](name= "Varcname", length=50)),
@AttributeOverride (name= "varcaddress",[email protected](name= "varcaddress", length=50)),
@AttributeOverride (name= "IntAge",[email protected](name= "IntAge", Nullable=false)) } )
Public UserId getId () {
return this.id;
}
public void SetId (UserId id) {
This.id = ID;
}
}
</span>
Package Com.sourcefour.bean;
Default Package

Import Javax.persistence.AttributeOverride;
Import Javax.persistence.AttributeOverrides;
Import Javax.persistence.Column;
Import Javax.persistence.EmbeddedId;
Import javax.persistence.Entity;
Import javax.persistence.Table;


/**
* User entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table (name= "T_USER_COMPOSITE_PK"
)

public class User implements Java.io.Serializable {


Fields

Private UserId ID;


Constructors

/** default Constructor */
Public User () {
}


/** Full Constructor */
Public User (UserId ID) {
This.id = ID;
}


Property accessors
@EmbeddedId

@AttributeOverrides ({
@AttributeOverride (name= "IntId",[email protected](name= "IntId", Nullable=false)),
@AttributeOverride (name= "Varcname",[email protected](name= "Varcname", length=50)),
@AttributeOverride (name= "varcaddress",[email protected](name= "varcaddress", length=50)),
@AttributeOverride (name= "IntAge",[email protected](name= "IntAge", Nullable=false)) } )

Public UserId getId () {
return this.id;
}

public void SetId (UserId id) {
This.id = ID;
}
}
2.2. Userid.java

[Java]
<span style= "FONT-SIZE:12PX;" >package Com.sourcefour.bean;
Default Package
Import Javax.persistence.Column;
Import javax.persistence.Embeddable;
/**
* UserId entity. @author MyEclipse Persistence Tools
*/
@Embeddable
public class UserId implements Java.io.Serializable {
Fields
private int intId;
Private String Varcname;
Private String varcaddress;
private int intAge;
Constructors
/** default Constructor */
Public UserId () {
}
/** Minimal Constructor * *
Public UserId (int intId, int intAge) {
This.intid = intId;
This.intage = IntAge;
}
/** Full Constructor */
Public UserId (int intId, string varcname, string varcaddress, int intAge) {
This.intid = intId;
This.varcname = Varcname;
this.varcaddress = varcaddress;
This.intage = IntAge;
}
Property accessors
@Column (name = "IntId", Nullable = False)
public int Getintid () {
return this.intid;
}
public void Setintid (int intId) {
This.intid = intId;
}
@Column (name = "Varcname", length = 50)
Public String Getvarcname () {
return this.varcname;
}
public void Setvarcname (String varcname) {
This.varcname = Varcname;
}
@Column (name = "Varcaddress", length = 50)
Public String getvarcaddress () {
return this.varcaddress;
}
public void setvarcaddress (String varcaddress) {
this.varcaddress = varcaddress;
}
@Column (name = "IntAge", Nullable = False)
public int getintage () {
return this.intage;
}
public void setintage (int intAge) {
This.intage = IntAge;
}
public boolean equals (Object other) {
if ((this = other))
return true;
if ((other = = null))
return false;
if (! ( Other instanceof UserId))
return false;
UserID Castother = (userid) Other;
Return (This.getintid () = = Castother.getintid ())
&& ((this.getvarcname () = = Castother.getvarcname ()) | | (This.getvarcname ()! = NULL
&& castother.getvarcname ()! = null && this.getvarcname (). Equals (Castother.getvarcname ()))
&& ((this.getvarcaddress () = = Castother.getvarcaddress ()) | | (This.getvarcaddress ()! = NULL
&& castother.getvarcaddress ()! = null && this.getvarcaddress (). Equals (
Castother.getvarcaddress ())) && (this.getintage () = = Castother.getintage ());
}
public int hashcode () {
int result = 17;
result = PNS * result + this.getintid ();
result = PNS * result + (getvarcname () = = null? 0:this.getvarcname (). Hashcode ());
result = PNS * result + (getvarcaddress () = = null? 0:this.getvarcaddress (). Hashcode ());
result = PNS * result + this.getintage ();
return result;
}
}</span>
Package Com.sourcefour.bean;

Default Package

Import Javax.persistence.Column;
Import javax.persistence.Embeddable;

/**
* UserId entity. @author MyEclipse Persistence Tools
*/
@Embeddable
public class UserId implements Java.io.Serializable {

Fields

private int intId;
Private String Varcname;
Private String varcaddress;
private int intAge;

Constructors

/** default Constructor */
Public UserId () {
}

/** Minimal Constructor * *
Public UserId (int intId, int intAge) {
This.intid = intId;
This.intage = IntAge;
}

/** Full Constructor */
Public UserId (int intId, string varcname, string varcaddress, int intAge) {
This.intid = intId;
This.varcname = Varcname;
this.varcaddress = varcaddress;
This.intage = IntAge;
}

Property accessors

@Column (name = "IntId", Nullable = False)
public int Getintid () {
return this.intid;
}

public void Setintid (int intId) {
This.intid = intId;
}

@Column (name = "Varcname", length = 50)
Public String Getvarcname () {
return this.varcname;
}

public void Setvarcname (String varcname) {
This.varcname = Varcname;
}

@Column (name = "Varcaddress", length = 50)
Public String getvarcaddress () {
return this.varcaddress;
}

public void setvarcaddress (String varcaddress) {
this.varcaddress = varcaddress;
}

@Column (name = "IntAge", Nullable = False)
public int getintage () {
return this.intage;
}

public void setintage (int intAge) {
This.intage = IntAge;
}

public boolean equals (Object other) {
if ((this = other))
return true;
if ((other = = null))
return false;
if (! ( Other instanceof UserId))
return false;
UserID Castother = (userid) Other;

Return (This.getintid () = = Castother.getintid ())
&& ((this.getvarcname () = = Castother.getvarcname ()) | | (This.getvarcname ()! = NULL
&& castother.getvarcname ()! = null && this.getvarcname (). Equals (Castother.getvarcname ()))
&& ((this.getvarcaddress () = = Castother.getvarcaddress ()) | | (This.getvarcaddress ()! = NULL
&& castother.getvarcaddress ()! = null && this.getvarcaddress (). Equals (
Castother.getvarcaddress ())) && (this.getintage () = = Castother.getintage ());
}

public int hashcode () {
int result = 17;

result = PNS * result + this.getintid ();
result = PNS * result + (getvarcname () = = null? 0:this.getvarcname (). Hashcode ());
result = PNS * result + (getvarcaddress () = = null? 0:this.getvarcaddress (). Hashcode ());
result = PNS * result + this.getintage ();
return result;
}

}

3. Problems that may still occur

Specifically, the problem is that the resulting list of queries is ' null ' (which I did not show on my machine this time).
If this problem occurs, then see this should be able to solve the problem, if not appear that is better, hehe!
But the individual feels that this should still be said. ^_^
Sometimes the query results list is ' null ', which is very confusing, can think of what is the reason?
Directly on the cause, Gaga ....
Cause: A field that is a federated primary key should theoretically not contain fields that might be empty.
Cause analysis: Depending on the reason, there is a null value in the corresponding table field in the entity bean.
Solution: You only need to leave a potentially empty field as part of the Federated primary Key.
Say the estimate dizzy head, directly to a case (the individual has always felt that the example is the best explanation of the problem).

Assuming that the varcname and varcaddress in the table are likely to be empty, others cannot be empty, then the mapping should look like this

User.java

[Java]
......
Private UserId ID;
Private String Varcname;
Private String varcaddress;
......
/*
Here add Varcname and varcaddress map content, ah or paste it out, anyway electronic file is not afraid of wood has place, Gaga ...
*/
@Column (name= "Varcname", length=50)
Public String Getvarcname () {
return this.varcname;
}
public void Setvarcname (String varcname) {
This.varcname = Varcname;
}
@Column (name= "varcaddress", length=50)
Public String getvarcaddress () {
return this.varcaddress;
}
public void setvarcaddress (String varcaddress) {
this.varcaddress = varcaddress;
}
......
Private UserId ID;
Private String Varcname;
Private String varcaddress;
......
/*
Here add Varcname and varcaddress map content, ah or paste it out, anyway electronic file is not afraid of wood has place, Gaga ...
*/
@Column (name= "Varcname", length=50)
Public String Getvarcname () {
return this.varcname;
}

public void Setvarcname (String varcname) {
This.varcname = Varcname;
}

@Column (name= "varcaddress", length=50)
Public String getvarcaddress () {
return this.varcaddress;
}

public void setvarcaddress (String varcaddress) {
this.varcaddress = varcaddress;
}

Userid.java

[Java] View plain copy print?
......
private int intId;
private int intAge;
......

Hibernate No primary key table (composite primary key) mapping

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.