Hello,
I've got this simple Book-class, whose objects should be stored in a
database:
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
@Entity
@NamedQuery(name = "findAllBooks", query = "SELECT OBJECT(bk) from
Book bk")
public class Book implements Serializable {
private Long id;
private String author;
private String title;
private boolean available;
// [...]
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
// [...]
}
I've written the following session bean to handle serialization of the
Book-objects:
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;
public class BookBean {
@PersistenceContext(unitName="books")
private EntityManager em;
@Resource
private UserTransaction utx;
private Book currentBook = new Book();
// [...]
public String submitBook() {
try {
utx.begin();
em.persist(currentBook);
utx.commit();
} catch (Exception e) {
try {
utx.rollback();
} catch (Exception e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
currentBook = null;
return "submitBook";
}
// [...]
}
The submitBook()-method of the BookBean is called by a .jsp-File from
which the user can create a new Book-object, which is stored in the
currentBook-object. However, when the program reaches the utx.commit()-
statement the following Exception is thrown:
java.lang.IllegalStateException: During synchronization a new object
was found through a relationship that was not marked cascade PERSIST.
at
oracle.toplink.essentials.internal.ejb.cmp3.base.RepeatableWriteUnitOfWork
$1.iterate(RepeatableWriteUnitOfWork.java:115)
[...]
Does anyone know why I get this strange exception although I haven't
used any relationships at all in my JSF-program?
Thanks for any help.
nax - 10 Aug 2007 12:44 GMT
Hello,
I've found the solution to my problem on my own.
The problem was that there were the two getter and setter methods
"Book getBook()" and "setBook(Book)" in my Book class. Because of this
methods the framework must have searched for a relationship between
two books, although I haven't specified a relationship with an
annotation in my class. After removing those two methods the program
worked as expected.
Sorry for bothering you.