1 Reply Latest reply on Mar 6, 2008 2:16 PM by onur.aktas

    Problem with Session EJB - Hibernate

      Hi, i am very new on Hibernate, and also EJB's. The problem is that i just want to save @ManyToOne related records based on different tables.

      Please tell me if i am following a WRONG architecture? And also when i execute mainline,
      i get an error of

      java.lang.RuntimeException: javax.transaction.RollbackException: [com.arjuna.ats.internal.jta.transaction.arjunacore.commitwhenaborted] [com.arjuna.ats.internal.jta.transaction.arjunacore.commitwhenaborted] Can't commit because the transaction is in aborted state
      .
      .
      .
      .
       at com.las.client.MainLine.main(MainLine.java:52)
      Caused by: java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.las.pojo.BookEntity.categoryEN -> com.las.pojo.CategoryEntity
       at
      


      I use MSSQL, PK Fields are identity.. Here are my codes..
      CATEGORY has many BOOKs


      Book Entity / Pojo
      @Entity
      @Table(name="BOOK")
      public class BookEntity implements Serializable {
       @Id @GeneratedValue
       @Column(name="ID")
       private Long id;
      
       @Column(name="NAME")
       private String name;
      
       @ManyToOne
       @JoinColumn(name="CATEGORYID")
       private CategoryEntity categoryEN;
      
       public BookEntity() {
       }
      
       public void setId(Long id) {
       this.id = id;
       }
      
       public Long getId() {
       return id;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      
       public String getName() {
       return name;
       }
      
       public void setCategoryEN(CategoryEntity categoryEN) {
       this.categoryEN = categoryEN;
       }
      
       public CategoryEntity getCategoryEN() {
       return categoryEN;
       }
      }
      

      Category Entity / Pojo
      @Entity
      @Table(name="CATEGORY")
      public class CategoryEntity implements Serializable {
       @Id @GeneratedValue
       @Column(name="ID")
       private Long id;
      
       @Column(name="NAME")
       private String name;
      
       @OneToMany(mappedBy="categoryEN")
       private Set<BookEntity> bookList;
      
       public CategoryEntity() {
      
       }
      
       public CategoryEntity(String name) {
       this.name = name;
       }
       public void setId(Long id) {
       this.id = id;
       }
      
       public Long getId() {
       return id;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      
       public String getName() {
       return name;
       }
      
       public void setBookList(Set<BookEntity> bookList) {
       this.bookList = bookList;
       }
      
       public Set<BookEntity> getBookList() {
       return bookList;
       }
      }

      BookEJB
      @Stateless(name="BookEJB")
      public class BookEJBBean implements BookEJB, BookEJBLocal {
      
       @PersistenceContext(unitName="lasDatabase")
       private EntityManager em;
      
       public BookEJBBean() {
      
       }
      
       public void Save(BookEntity BookEN) {
       em.persist(BookEN);
       }
       public void Delete(BookEntity BookEN) {
       }
       public List<BookEntity> getAll() {
       return em.createQuery("from BOOK")
       .getResultList();
       }
       public BookEntity getSimple(Long id) {
       return em.find(BookEntity.class, id);
       }
      }


      CategoryEJB
      @Stateless(name="CategoryEJB")
      public class CategoryEJBBean implements CategoryEJB, Serializable {
      
       @PersistenceContext(unitName="lasDatabase")
       private EntityManager em;
      
       public CategoryEJBBean() {
      
       }
      
       public void Save(CategoryEntity categoryEN) {
       em.persist(categoryEN);
       }
       public void Delete(CategoryEntity categoryEN) {
       }
       public List<CategoryEntity> getAll() {
       return em.createQuery("from CATEGORY")
       .getResultList();
       }
       public CategoryEntity getSimple(Long id) {
       return em.find(CategoryEntity.class, id);
       }
      }


      CLIENT CODE

      public class MainLine {
       public MainLine() {
       }
      
       public static void main(String[] args) {
       MainLine mainLine = new MainLine();
      
       try {
       Context context = new InitialContext();
      
       CategoryEJB categoryRemote = (CategoryEJB)context.lookup("lasejb/CategoryEJB/remote");
       BookEJB bookRemote = (BookEJB)context.lookup("lasejb/BookEJB/remote");
      
       CategoryEntity categoryEN = new CategoryEntity("Horror");
       categoryRemote.Save(categoryEN);
      
       BookEntity bookEN = new BookEntity();
       bookEN.setIsbn("123-321-333");
       bookEN.setName("ABC");
       bookEN.setCategoryEN(categoryEN);
      
       categoryRemote.Save(categoryEN);
       bookRemote.Save(bookEN);
       }
       catch(Exception ex) {
       ex.printStackTrace();
       }
       }
      }