2011-11-05 67 views
2

我正在使用EJB在netbeans上寫一個CRUD演示代碼..當我編寫一個可以將數據保存到數據庫的代碼時,沒有錯誤和異常,但數據不保存在數據庫中 代碼:JPA2:我堅持一個實體,沒有例外,但數據沒有保存在數據庫中

EntityBean的:

@Entity 
    @Table(name = "book_book") 
    @XmlRootElement 
    @NamedQueries({ 
     @NamedQuery(name = "BookBook.findAll", query = "SELECT b FROM BookBook b"), 
     @NamedQuery(name = "BookBook.findByIsbn", query = "SELECT b FROM BookBook b WHERE b.isbn = :isbn"), 
     @NamedQuery(name = "BookBook.findByDealTotal", query = "SELECT b FROM BookBook b WHERE b.dealTotal = :dealTotal"), 
     @NamedQuery(name = "BookBook.findBySaleTotal", query = "SELECT b FROM BookBook b WHERE b.saleTotal = :saleTotal"), 
     @NamedQuery(name = "BookBook.findBySalePrice", query = "SELECT b FROM BookBook b WHERE b.salePrice = :salePrice"), 
     @NamedQuery(name = "BookBook.findByDealPrice", query = "SELECT b FROM BookBook b WHERE b.dealPrice = :dealPrice")}) 
    public class BookBook implements Serializable { 
     private static final long serialVersionUID = 1L; 
     @Id 
     @Basic(optional = false) 
    // @NotNull 
     @Column(name = "isbn") 
     private Long isbn; 
     @Column(name = "deal_total") 
     private Integer dealTotal; 
     @Column(name = "sale_total") 
     private Integer saleTotal; 
     @Column(name = "sale_price") 
     private Integer salePrice; 
     @Column(name = "deal_price") 
     private Integer dealPrice; 
     @OneToMany(mappedBy = "isbn") 
     private List<BookBookdetail> bookBookdetailList; 

DAClass(不是會話Bean):

EntityManagerFactory factory=Persistence.createEntityManagerFactory("BookXPU"); 
    EntityManager em=factory.createEntityManager(); 

     public long createBook(Long isbn, Integer dealTotal, Integer saleTotal, Integer salePrice, Integer dealPrice){ 
     BookBook book=new BookBook(); 
     book.setDealTotal(dealTotal); 
     book.setSalePrice(salePrice); 
     book.setDealTotal(dealTotal); 
     book.setDealPrice(dealPrice); 
     book.setIsbn(isbn); 
     List<BookBookdetail> list=new ArrayList<BookBookdetail>(); 
     book.setBookBookdetailList(null); 
     em.persist(book); 


     Query query =em.createQuery("SELECT b FROM BookBook b WHERE b.isbn = :isbn"); 
     query.setParameter("isbn",book); 
     List list1=query.getResultList(); 
      for (Iterator it = list1.iterator(); it.hasNext();) { 
       BookBookDetail book1 = (BookBookDetail)it.next(); 
       list.add(book1); 
      } 
      book.setBookBookdetailList(list); 
     em.merge(book); 
     return book.getIsbn(); 


     } 

謝謝...

+0

我嘗試在sessionbean中測試此代碼並取得成功... – user996505

+0

因此,您確實嘗試過並且能夠正常工作?這段代碼有什麼問題嗎?您無法在數據庫中看到結果或在代碼中使用查詢? –

+0

如果這個代碼在一個普通的java類中......我無法看到數據庫中的結果......我不得不將常見的類更改爲無狀態的sessionbean ......我可以看到結果然後..但你能告訴我怎樣才能改變代碼,以便在普通java類中取得成功 – user996505

回答

1

您的實體沒有被保留,因爲您沒有將實體刷新到您的數據庫

如果您使用的代碼在Java SE比你應該做的:

EntityTransaction tx = em.getTransaction(); 
tx.begin(); 
// persist, merge, etc. - operations which needs to be in the Tx... 
tx.close(); 

沒有它,你不給一個跡象,表明你已經改變了數據應被刷新到底層數據庫。

當您將代碼移動到EJB中時,您正在使用JTA事務(隱式地)。因此,EntityManager的事務是JTA事務的一部分。默認情況下,EJB的每個方法都是單個JTA事務。

現在,通過將您的類標記爲EJB,將這些信息組合起來發生了,您已將數據刷新到底層數據庫

HTH。

+0

@ user996505你是否設法解決了這個問題? –

相關問題