我正在使用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();
}
謝謝...
我嘗試在sessionbean中測試此代碼並取得成功... – user996505
因此,您確實嘗試過並且能夠正常工作?這段代碼有什麼問題嗎?您無法在數據庫中看到結果或在代碼中使用查詢? –
如果這個代碼在一個普通的java類中......我無法看到數據庫中的結果......我不得不將常見的類更改爲無狀態的sessionbean ......我可以看到結果然後..但你能告訴我怎樣才能改變代碼,以便在普通java類中取得成功 – user996505