How to add a one-to-many association based on two columns where one has a FIXED VALUE

Hi everybody,

I need to map a collection table linked to the main one with two fields. One of them is fixed.

This is how I’d like to do:

@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(
	name="lt_customlabel", 
	schema = LTDBConfig.LTSchema,
	joinColumns= {
		@JoinColumn(name = "fieldid", referencedColumnName = "id", insertable = false, updatable = false, nullable = false),
		@JoinColumn(name ="prefixid", FIXED VALUE = <package variable>)			
	}
)
@OrderColumn(name = "ID", insertable = false, updatable = false, nullable = false)
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
private Set<CustomLabel> customLabels = new HashSet<>();

Do you have any suggestion? I could use @JoinColumnsOrFormulas, but it looks like is not accepted inside Collections.

Thanks, ciao.
Luca

Using @JoinColumnsOrFormulas works with associations, so you need to replace the element collection with a @OneToMany association.

Hello,

@JoinColumnsOrFormula does no fit my requirements as it is not possible to associate the column in the child table.

@JoinColumnsOrFormulas({
	@JoinColumnOrFormula(
		formula = @JoinFormula(
			value = "LU", 
			referencedColumnName = "prefixID"
		)
	),
	@JoinColumnOrFormula(
		column = @JoinColumn(
			name = "fieldID", 
			referencedColumnName = "ID"
		)
	)
})

The sample above raises an error as the referencedColumnName is expected to be in the parent table.

This is the solution that works for me, don’t know if it is the best one.

@Expose	
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "fieldID")
@Where(clause = "prefixID = '" + LUDBConfig.LUPrefix + "'")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
private Set<CustomLabel> customLabels = new HashSet<>();