Reputation: 1260
I'm working on a jpa project where I persist multiple instances of a Band object which contains an ArrayList of Date objects.
I want to query the database to obtain Bands, which ArrayLists contain the specified Date.
Is this possible? I am aware my entire design could be bad but I'm hoping this is possible, as I am aware that you need a get method to use object parameters, so the method must be called somewhere.
I want to use something along these lines:
Date d = new Date();
List bands = em.createQuery("select b from Band b where b.dates.contains(d)").getResultList();
//I am aware I may need to set up d as an argument.
This is the Band entity as requested. Assume package and imports are correct.
@Entity
@Table(name = "BAND")
public class Band extends SuperUser {
@Column(name = "PHONENUMBER")
private String phoneNumber;
@Column(name = "BIOGRAPHY")
private String biography;
@Column(name = "DATES")
private ArrayList<Date> dates = new<Date> ArrayList();
public Band() {
}
public Band(String n, String e, String p, String ph_no, String bio) {
super(n, e, p);
this.phoneNumber = ph_no;
this.biography = bio;
}
public void setPhoneNumber(String p) {
this.phoneNumber = p;
}
public String getPhoneNumber() {
return this.phoneNumber;
}
public void setBoigraphy(String b) {
this.biography = b;
}
public String getBiography() {
return this.biography;
}
public void setDate(Date d) {
dates.add(d);
}
public void cancelDate(Date d) {
while (dates.remove(d)) {
}
}
public ArrayList getDates() {
return dates;
}
}
EDIT: I got it working. I Didn't know about elementcollections and temporal types, changing the date list to:
@ElementCollection
@Temporal(TemporalType.DATE)
private List<Date> dates = new <Date>ArrayList();
fixed it.
Upvotes: 1
Views: 293
Reputation: 8807
I think what you're looking for is "Member Of": http://www.objectdb.com/java/jpa/query/jpql/collection#NOT_MEMBER_OF_
select b from Band b where :d member of b.dates
Upvotes: 2