Question & Answer
Question
This technote addresses how the UML-to-JPA Transformation in IBM Rational Software Architect for WebSphere Edition can generate Primary Key classes, also known as "Composite Keys".
Cause
Generation of the annotation @IdClass from a UML model using the UML-to-JPA Transformation is not supported.
Answer
In the Java Persistence Architecture, it is possible to generate composite keys in two ways:
A. Using @IdClass:
1. Create a serializable Java class MyEntityPK that serves as composite key
2. Reference such Java class from the entity class using the annotation @IdClass(MyEntityPK.class) in front of the entity class
B. Using @EmbeddedId:
1. Define a serializable Java class MyEntityPK that serves as composite key with annotation @Embeddable in front of the class
2. Reference such Java class from the entity class using the annotation @EmbeddedId in front of the fields or accessors that refer to the composite key
In IBM Rational Software Architect 7.5 and 7.5.1 the Method B is supported and this construct can be modeled in the following way:
1. Apply the profile Java Persistence API Transformation to a new UML Model
2. Create a package package1 inside the model
3. Create a class MyEntity inside the package
4. Apply the stereotype «Entity» to the class
5. Add more than one attribute to the class, specify the name and type of the attributes
6. Give to more than one attribute the stereotype «Id»
The resulting class will look similar to this:

The above model generates code like the following (generated comments have been omitted to improve readability):
Class package1.MyEntity package package1;
import javax.persistence.Entity;
import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.NamedQuery;
import javax.persistence.NamedQueries;
@Entity
@NamedQueries( {
@NamedQuery(name = "MyEntity.findByname", query = "select obj from MyEntity where obj.name = :name"),
@NamedQuery(name = "MyEntity.findByaddress", query = "select obj from MyEntity where obj.address = :address") })
public class MyEntity implements Serializable {
private static final long serialVersionUID = 0;
public MyEntity() {
}
@EmbeddedId
private MyEntityPK pk;
public void setPk(MyEntityPK pk) {
this.pk = pk;
}
public MyEntityPK getPk() {
return pk;
}
}
Class package1.MyEntityPk package package1;
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class MyEntityPK implements Serializable {
private static final long serialVersionUID = 0;
public MyEntityPK() {
}
private String name;
public String getName() {
// begin-user-code
return name;
// end-user-code
}
public void setName(String name) {
// begin-user-code
this.name = name;
// end-user-code
}
private String address;
public String getAddress() {
// begin-user-code
return address;
// end-user-code
}
public void setAddress(String address) {
// begin-user-code
this.address = address;
// end-user-code
}
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof MyEntityPK))
return false;
MyEntityPK pk = (MyEntityPK) obj;
if (!(name.equals(pk.name)))
return false;
if (!(address.equals(pk.address)))
return false;
return true;
}
public int hashCode() {
return name.hashCode() + address.hashCode();
}
}
See the Product Help page in the Related infomation section for more information on the meaning of the above stereotypes.
Related Information
Was this topic helpful?
Document Information
Modified date:
29 September 2018
UID
swg21377916