-
Notifications
You must be signed in to change notification settings - Fork 70
howto_enable_composite_primary_keys_in_entity
In order to enable Composite Primary Keys in entity in CobiGen, the below approach is suggested
The templates in CobiGen have been enhanced to support Composite primary keys while still supporting the default devonfw/Cobigen
values with Long id.
Also, the current generation from Entity still holds good - right click from an Entity object, CobiGen → Generate will show the CobiGen wizard relative to the entity generation.
After generating, below example shows how composite primary keys can be enabled.
@Entity
@Table(name = "employee")
public class EmployeeEntity {
private CompositeEmployeeKey id;
private String name;
private String lastName;
@Override
@EmbeddedId
public CompositeEmployeeKey getId() {
return id;
}
@Override
public void setId(CompositeEmployeeKey id) {
this.id = id;
}
.
.
.
public class CompositeEmployeeKey implements Serializable {
private String companyId;
private String employeeId;
Once the generation is complete, implement PersistenceEntity<ID>
.java in the EmployeeEntity
and pass the composite primary key object which is CompositeEmployeeKey
in this case as the parameter ID
.
import com.devonfw.module.basic.common.api.entity.PersistenceEntity;
@Entity
@Table(name = "employee")
public class EmployeeEntity implements PersistenceEntity<CompositeEmployeeKey> {
private CompositeEmployeeKey id;
private String name;
private String lastName;
Also, the modificationCounter
methods needs to be implemented from the interface PersistenceEntity<ID>
. The sample implementation of the modification counter can be referred below.
@Override
public int getModificationCounter() {
if (this.persistentEntity != null) {
// JPA implementations will update modification counter only after the transaction has been committed.
// Conversion will typically happen before and would result in the wrong (old) modification counter.
// Therefore we update the modification counter here (that has to be called before serialization takes
// place).
this.modificationCounter = this.persistentEntity.getModificationCounter();
}
return this.modificationCounter;
}
@Override
public void setModificationCounter(int version) {
this.modificationCounter = version;
}
Disclaimer
If you discover any documentation bugs or would like to request new content, please raise them as an issue or create a pull request. Contributions to this wiki are done through the main repo under the folder documentation.
License
This documentation is licensed under the Creative Commons License (Attribution-NoDerivatives 4.0 International
)