我從GlassFish3。所以開始的Java EE6嘗試一些例子的流程困惑,我創建了一個實體類,它看起來像這樣...與構造
@Entity
@Table(name="Book")
public class Book implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable=false)
private String name;
@Column(nullable=false)
private String isbn;
private String description;
public Book()
{
// Empty constructor to facilitate construction.
System.out.println("The variables have not been initialized...Please initialize them using the Setters or use the provided constructor");
}
public Book(String name, String isbn, String description) {
this.name = name;
this.isbn = isbn;
this.description = description;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return this.name + " - " + this.isbn;
}
@PrePersist
public void printPrePersist(){
System.out.println("Persisting the book "+this.name);
}
@PostPersist
public void printPostPersist(){
System.out.println("Persisted the book "+this.name);
}
}
,我試圖堅持它像這樣...
public class MainClass
{
public static void main(String[] args){
Book book = new Book("Effective Java","ISBN - 1234415","A very good book on Java");
Book book2 = new Book("Learning Java EE","ISBN - 1233415","A good book for Java EE beginners");
// These are the necessary classes
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersistenceAppPU");
EntityManager em = emf.createEntityManager();
// Persist the book here
EntityTransaction etx = em.getTransaction();
etx.begin();
em.persist(book);
em.persist(book2);
etx.commit();
em.close();
emf.close();
System.out.println("The two books have been persisted");
}
}
它仍然存在,但是當我跑,我看到一個輸出如下...
The variables have not been initialized...Please initialize them using the Setters or use the provided constructor
Persisting the book Effective Java
The variables have not been initialized...Please initialize them using the Setters or use the provided constructor
Persisting the book Learning Java EE
Persisted the book Learning Java EE
Persisted the book Effective Java
The variables have not been initialized...Please initialize them using the Setters or use the provided constructor
The variables have not been initialized...Please initialize them using the Setters or use the provided constructor
The variables have not been initialized...Please initialize them using the Setters or use the provided constructor
The variables have not been initialized...Please initialize them using the Setters or use the provided constructor
[EL Info]: 2012-05-10 12:01:19.623--ServerSession(17395905)--file:/C:/Users/raviteja.s/Documents/NetBeansProjects/PersistenceApp/src/_PersistenceAppPU logout successful
The two books have been persisted
我不明白,爲什麼有這麼多的默認構造函數調用時,沒有一個由我做... ...?
有人可以解釋我在樣品中的流動情況嗎?
似乎像EntityManager使用默認構造函數創建新的書籍對象。如果你在默認的構造函數中拋出一個異常,你可以看到正在調用構造函數的整個Stack(或使用調試器),只需添加 try {throw new Exception(); } catch(Exception e){e.printStackTrace();}給你的構造函數並再次運行 – outofBounds
@outofBounds或者你可以使用靜態的'Thread.dumpStack()' – yshavit
@yshavit thx does'n know this。它的變化更容易,更清潔 – outofBounds