Hibernate non-primary key table (composite primary key) ing

Source: Internet
Author: User

1. Why composite primary key ing?
In reality, we may encounter many tables that do not have a primary key. What kind of results will we use after ing them? Can we get what we want? The expected result is not obtained, and the following error may be returned:

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

The result shows that the Hibernate ing table requires a primary key.

Therefore, composite primary key ing emerged.

2. Notes
The composite primary key uses an embedded class as the primary key. Therefore, you need to use the @ Id and @ Embeddable annotations. another way is to use the @ EmbeddedId annotation. of course, another method is to use the @ IdClass annotation. For details, see [hibernate-annotations-3.4.0.GA 2.2.6. ing compound primary key and foreign key ].

Of course, you only need to select one of them. Pai_^

Note that the dependent classes must implement serializable and the equals ()/hashCode () method.

Here is 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;
// Constructors
/** Default constructor */
Public User (){
}
/** Full constructor */
Public User (UserId id ){
This. id = id;
}
// Property accessors
@ EmbeddedId
@ AttributeOverrides ({
@ AttributeOverride (name = "intId", column = @ Column (name = "intId", nullable = false )),
@ AttributeOverride (name = "varcName", column = @ Column (name = "varcName", length = 50 )),
@ AttributeOverride (name = "varcAddress", column = @ Column (name = "varcAddress", length = 50 )),
@ AttributeOverride (name = "intAge", column = @ Column (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;


// Constructors

/** Default constructor */
Public User (){
}


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


// Property accessors
@ EmbeddedId

@ AttributeOverrides ({
@ AttributeOverride (name = "intId", column = @ Column (name = "intId", nullable = false )),
@ AttributeOverride (name = "varcName", column = @ Column (name = "varcName", length = 50 )),
@ AttributeOverride (name = "varcAddress", column = @ Column (name = "varcAddress", length = 50 )),
@ AttributeOverride (name = "intAge", column = @ Column (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 = 37 * result + this. getIntId ();
Result = 37 * result + (getVarcName () = null? 0: this. getVarcName (). hashCode ());
Result = 37 * result + (getVarcAddress () = null? 0: this. getVarcAddress (). hashCode ());
Result = 37 * 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 = 37 * result + this. getIntId ();
Result = 37 * result + (getVarcName () = null? 0: this. getVarcName (). hashCode ());
Result = 37 * result + (getVarcAddress () = null? 0: this. getVarcAddress (). hashCode ());
Result = 37 * result + this. getIntAge ();
Return result;
}

}

3. Possible Problems

Specifically, the problem is that the list of queried results is 'null' (which I did not show during this test on my machine ).
If this problem occurs, you can solve the problem by seeing it. If it does not appear, it will be better!
But I personally think this should be said. Pai_^
Sometimes the list of queried results is 'null', which is confusing. Why?
For the above reason, grack .......
Cause: fields used as joint primary keys should not theoretically contain fields that may be null.
Cause Analysis: Based on the cause, it indicates that a table field corresponding to some of the entity beans has a null value.
Solution: you only need to leave fields that may be null as part of the Union primary key.
Let's look at the estimate. (I have always thought that the example is the best explanation of the problem ).

Assume that varcName and varcAddress in the table may be empty, and none of them can be empty. The ing should be like this.

User. java

[Java]
......
Private UserId;
Private String varcName;
Private String varcAddress;
......
/*
Add the varcName and varcAddress ing content here, and paste the content. The electronic files are not afraid of any other places ......
*/
@ 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;
Private String varcName;
Private String varcAddress;
......
/*
Add the varcName and varcAddress ing content here, and paste the content. The electronic files are not afraid of any other places ......
*/
@ 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;
......

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.