I have the following piece of Code:
static ConcurrentLinkedQueue<Band> bands;
...
public int perform(EntityManager em) {
Band b = em.merge(bands.poll());
try {
String statement = "UPDATE Band AS b " +
"SET b.name='The "+b.getName()+"'"+
" WHERE b.id="+b.getId();
Query query = em.createQuery(statement);
query.executeUpdate();
statement = "UPDATE Album AS a " +
"SET a.name='The "+b.getName()+"'"+
" WHERE a.id="+b.getAlbums().get(0).getId();
query = em.createQuery(statement);
query.executeUpdate();
if (b.getTour()!=null) {
statement = "UPDATE Concert AS c " +
"SET c.city='London' "+
"WHERE c.id="+b.getTour().getConcerts().get(0).getId();
query = em.createQuery(statement);
query.executeUpdate();
}
em.refresh(b);
bands.add(b);
return 0;
}
catch (Exception e) {
e.printStackTrace();
return -1;
}
}
The Entity Band looks like this:
@Entity
@Table(name="JBT_Band")
public class Band {
private Long id;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private List<Artist> members;
@OneToMany(cascade=ALL, mappedBy = "band")
public List<Artist> getMembers() {
return members;
}
public void setMembers(List<Artist> members) {
this.members = members;
}
private Tour tour;
@OneToOne(cascade=ALL, optional = true, mappedBy = "band")
public Tour getTour() {
return tour;
}
public void setTour(Tour tour) {
this.tour = tour;
}
private List<Album> albums;
@OneToMany(cascade=ALL, mappedBy = "band")
public List<Album> getAlbums() {
return albums;
}
public void setAlbums(List<Album> albums) {
this.albums = albums;
}
private int scenario;
public int getScenario() {
return scenario;
}
public void setScenario(int scenario) {
this.scenario = scenario;
}
}
The method perform() is processed by up to 10 Threads at the same time.
Each thread retrieves an entity 'Band' from a static and threadsafe
queue and updates the according table and some related tables by
JPQL-Queries.
The problem is, that after refresh() the lists 'albums' and 'members'
are set to NULL which causes NullPointerExceptions in other places. Is
this a bug in OpenJPA? (Code works in Hibernate and TopLink)
Regards,
Seb