3

I'm trying to perform a query to find cars by their foo property. The properties are stored in a different table.

I have two classes

@Embeddable
@Table(name = "PROPERTY")
public class Property {

  @Column(name = "type", nullable = false)
  private String type;

  @Column(name = "string_value", nullable = true)
  private String stringValue;

  ...
}


@Entity
@Table(name = "CAR")
public class Car {

  @Id
  ...
  private String id;

  @ElementCollection(fetch = FetchType.EAGER)
  @Fetch(FetchMode.SUBSELECT)
  @CollectionTable(name = "PROPERTY", joinColumns = @JoinColumn(name = "car_ID") )
  private Set<Property> properties = new HashSet<Property>();

  ...
}

I'm trying to perform a query

QueryDsl:

.from(car)
.leftJoin(car.properties, foo)
.on(foo.type.eq("foo"))
.where(predicate)

Resulting HQL:

select 
  car
from 
  com....Car car
left join 
  car.properties as foo with foo.type = :a1
where 
  ...

This doesn't work because of: https://hibernate.atlassian.net/browse/HHH-2772

Before that, it was possible to write HQL:
SELECT cat FROM Cat cat LEFT JOIN cat.kittens as kitten WITH kitten.owner=:owner
Now the HQL is raising an exception:
org.hibernate.hql.ast.InvalidWithClauseException: with clause can only reference columns in the driving table
Workaround is to explicitly use primary key (ownerId):
SELECT cat FROM Cat cat LEFT JOIN cat.kittens as kitten WITH kitten.owner.ownerId=:ownerId

The problem is that I don't have the ownerId, or an owner, since it's an element collection.

If I were to turn the element collection into a @oneToMany @manyToOne, the property could not longer be embeddable and would require an id. This is not an option. (I can't define a composite ID (this is a requirement), and I don't want to add a new column )

What do you recommend?
Is it possible to add the Car or Car Id as a field into an embaddable class?
Can I create the criteria in a different way?
I'm interested in any workaround that doesn't require database changes. (Hibernate changes or ok)

Thank you

Jakabfi Attila
  • 367
  • 3
  • 16

0 Answers0