0

I have two entity

@Entity
@Table(name = "Student")
class Student{
@Id
@Column(name = "Id")
private Integer id;

@Column(name = "name")
private String name;

@OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(referencedColumnName = "Id",foreignKey = @ForeignKey(name = "Id"))
private List<Address> addresses;

and

@Entity
@Table(name = "Address")
class Address{
@Id
@Column(name = "address_id")
private Integer addressId;

@Column(name = "city_name")
private String cityName;

@Column(name = "Id")
private Integer id;

trying to join above two entity

Specification<Student> specification = new Specification<Student>(){
@Override
public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query,CriteriaBuilder cb)
{
  query.distinct(true);
  Join<Student,Address> join = root.join("addresses");
  return cb.equal(join.get("id"),id);
}
repo.findAll(specification);

but the JPA is generating wrong query

select distict student0_.id as id1_5_,
student0_.name as name2_5_
from Student student0_
inner join Address addresses1_ on student0_.id = *addresses1_.addresses_id*

the issue i am getting is invalid column name "addresses_id". Couldn't find out why it is appending joinColumn with id.

1 Answer 1

0

The problem is in the following OneToMany definition. The @JoinColumn is adding the addresses_id column.

@OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(referencedColumnName = "Id",foreignKey = @ForeignKey(name = "Id"))
private List<Address> addresses;

Try

@OneToMany(mappedBy = "student", fetch = FetchType.LAZY,cascade = CascadeType.ALL)
private Set<Address> addresses;

The key is to use mappedBy. Also I my code, which is jHipster generated, uses a Set - List may also work.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.