I have two entity classes, FischEBBean and AquariumEBBean, which have a many-to-one relationship. Both classes use composite primary keys. When executing the findByAquarium method, I receive the following exception:
java.lang.IllegalArgumentException: Can not set java.lang.Long field testweb.eb.AquariumEBPK.id to testweb.eb.AquariumEBBean
Here are the relevant parts of my code:
FischEBBean.java
@Entity
@DynamicUpdate
@DynamicInsert
@SelectBeforeUpdate
@Table(name = "TEST_FISCH")
@IdClass(FischEBPK.class)
@NamedQuery(name = "FischEB.findByPrimaryKey", query = "from FischEBBean where jva = :jva and id=:id")
@NamedQuery(name = "FischEB.findByAquarium", query = "from FischEBBean f where f.aquarium = :aquarium")
public class FischEBBean extends TestEJBPersistentEntity implements FischEB {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_TEST_FISCH")
@SequenceGenerator(name = "SEQ_TEST_FISCH", sequenceName = "SEQ_TEST_FISCH", allocationSize = 2)
@Column(unique = true, nullable = false)
public Long id;
@Id
public Long jva;
private String name;
@ManyToOne(targetEntity = AquariumEBBean.class, fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "AQUARIUM_ID", referencedColumnName = "id"),
@JoinColumn(name = "AQUARIUM_JVA", referencedColumnName = "jva")
})
private AquariumEB aquarium;
// Getters, Setters, Equals, Hashcode
}
AquariumEBBean.java
@Entity
@DynamicUpdate
@DynamicInsert
@SelectBeforeUpdate
@Table(name = "TEST_AQUARIUM")
@IdClass(AquariumEBPK.class)
@NamedQuery(name = "AquariumEB.findByPrimaryKey", query = "from AquariumEBBean where jva = :jva and id=:id")
public class AquariumEBBean extends TestEJBPersistentEntity implements AquariumEB {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_TEST_AQUARIUM")
@SequenceGenerator(name = "SEQ_TEST_AQUARIUM", sequenceName = "SEQ_TEST_AQUARIUM", allocationSize = 2)
@Column(unique = true, nullable = false)
public Long id;
@Id
public Long jva;
private String name;
@OneToMany(mappedBy = "aquarium", targetEntity = FischEBBean.class, fetch = FetchType.LAZY)
private Set<FischEB> fische;
// Getters, Setters, Equals, Hashcode
}
FischEBPK.java
public class FischEBPK implements Serializable {
public Long id;
public Long jva;
// Getters, Setters, Equals, Hashcode
}
AquariumEBPK.java
public class AquariumEBPK implements Serializable {
public Long id;
public Long jva;
// Getters, Setters, Equals, Hashcode
}
public List<FischEB> findByAquarium(final AquariumEB aquariumEB) throws TestFinderException{
final TypedQuery<FischEB> query = getEntityManager().createNamedQuery("FischEB.findByAquarium", FischEB.class);
query.setHint("org.hibernate.cacheable", Boolean.TRUE);query.setParameter("aquarium",aquariumEB);
onBeforeQueryExecute(query, AquariumEB.class);
try {final List<FischEB> eb2find = query.getResultList();
if (eb2find == null) {
throw new FinderException("FischEB.findByAquarium(");
}
return eb2find;
} catch (Exception e) {throw new TestFinderException(e, query);}
}
When executing this method, the exception occurs. I suspect the issue is with passing the aquariumEB parameter in the Named Query FischEB.findByAquarium. The query might be trying to set an AquariumEBBean instance to a field that expects a Long value.
Question
How can I fix this issue? Do I need to change anything in the Named Queries or primary keys? Any help or hints would be greatly appreciated. I update Jboss 7.4 to 8. Thank you!