2012-05-10 47 views
1

我從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 

我不明白,爲什麼有這麼多的默認構造函數調用時,沒有一個由我做... ...?

有人可以解釋我在樣品中的流動情況嗎?

+0

似乎像EntityManager使用默認構造函數創建新的書籍對象。如果你在默認的構造函數中拋出一個異常,你可以看到正在調用構造函數的整個Stack(或使用調試器),只需添加 try {throw new Exception(); } catch(Exception e){e.printStackTrace();}給你的構造函數並再次運行 – outofBounds

+1

@outofBounds或者你可以使用靜態的'Thread.dumpStack()' – yshavit

+0

@yshavit thx does'n know this。它的變化更容易,更清潔 – outofBounds

回答

1

JPA使用不帶參數的構造函數來實例化實體,然後將這些實體中的字段綁定到對應的映射表和列。

你看到的那些輸出是JPA每次處理實體時爲你做的調用。

+0

你能解釋爲什麼只有4個打印結束,而不是6或8 ..有3個領域和一個身份。請你詳細說明構造函數調用的是什麼點。 – Flash

+0

對不起,但我不記得JPA中使用的流程:)如果你感興趣,應該使用'Thread.dumpStack()'建議yshavit。但請注意,打印在構造函數中使用,而不是在字段的getters/setter中使用......由於有2本書,我可能認爲每個實體的構造函數都由JPA調用兩次。 – javatutorial