techi
techi

Reputation: 133

how to set a value to composite primary key in hibernate application

I have a Java bean/model class with JPA annotation as below. This class has a composite key as shown below. In DAO.java how do I set the key to this composite key?

@EmbeddedId
@AttributeOverrides({
    @AttributeOverride(name = "ratioFunctionId", column = @Column(name = "ratio_function_id", nullable = false, scale = 0)),
    @AttributeOverride(name = "expressionId", column = @Column(name = "expression_id", nullable = false, scale = 0))
})
public RatioFunctionExpressionId getId() {
    return this.id;
}

public void setId(RatioFunctionExpressionId id) {
    this.id = id;
}

Upvotes: 0

Views: 2024

Answers (1)

Xavi López
Xavi López

Reputation: 27880

In order to set a value for this attribute, just create a new instance and assign it:

RatioFunctionExpressionId newId = new RatioFunctionExpressionId();
newId.setRatioFunctionId(aFunctionId);
newId.setExpressionId(anExpressionId);
aRatioFunctionExpression.setId(newId);

Remember that being RatioFunctionExpressionId an @Embeddable, it doesn't need an id of its own, because it doesn't represent an Entity on its own. It's only meant to provide grouping of property definitions for easy composition and modularity.

Upvotes: 1

Related Questions