Luciano Greiner
Luciano Greiner

Reputation: 183

Hibernate OneToMany JoinColmuns and Constant Value

I have a generic relationship between Data and Parameter like this:

enum ParameterType { RISK, ASSET }

class Data {

   Long id;

   @OneToMany
   @JoinColumns({
     @JoinColumn(name="data_id")
     @// Restrict DataParameter.type = 'RISK'
   })
   Set<DataParameter> risks;

   @OneToMany
   @JoinColumns({
     @JoinColumn(name="data_id")
     @// Restrict DataParameter.type = 'ASSET'
   })
   Set<DataParameter> assets;
}

class DataParameter {

   Long id;

   ParameterType type;

   @ManyToOne
   Parameter parameter;
   @Temporal
   Date date;
   ...

}

How would manage to restrict this relationship using considering the ParameterType? I am using Hibernate 4.0

Thank you!

Upvotes: 2

Views: 3880

Answers (1)

Mikko Maunu
Mikko Maunu

Reputation: 42114

You can limit it by giving condition (SQL) in Hibernate specific @Where annotation. In this case following should work:

   //other annotations as they where 
   @Where(clause="type=0")
   Set<DataParameter> risks;

And then same with value 1 for assets.

Upvotes: 2

Related Questions