Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / JavaBeans / August 2007

Tip: Looking for answers? Try searching our database.

Java Server Faces and Java Persistence API - Newbie problem

Thread view: 
nax - 09 Aug 2007 18:49 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.