Java 与构造器的流程相混淆

Java 与构造器的流程相混淆,java,jakarta-ee,Java,Jakarta Ee,我正在试用一些从JavaEE6开始使用GlassFish3的例子。所以,我创建了一个实体类,基本上看起来像这样 @Entity @Table(name="Book") public class Book implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @Column(nullable=false) private S

我正在试用一些从JavaEE6开始使用GlassFish3的例子。所以,我创建了一个实体类,基本上看起来像这样

@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");
    }
}
我试着这样坚持下去

@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
我不明白,为什么会有这么多默认构造函数调用,而我却没有一个


有人能解释一下我的示例中的流是如何出现的吗?

JPA使用一个没有参数的构造函数来实例化实体,然后将这些实体中的字段绑定到对应的映射表和列


您看到的这些输出是JPA每次操作实体时为您执行的调用。

JPA使用一个不带参数的构造函数来实例化实体,然后将这些实体中的字段绑定到对应的映射表和列


您看到的这些输出是JPA每次操作实体时为您执行的调用。

似乎EntityManager正在使用默认构造函数创建新书对象。如果在默认构造函数中抛出异常,您可以看到调用构造函数的整个堆栈(或使用调试器),只需将try{throw new exception();}catch(exception e){e.printStackTrace();}添加到构造函数中并运行它again@outofBounds或者您可以只使用static
Thread.dumpStack()
@yshavit-thx不知道这一点。像EntityManager这样的更简单、更干净的浏览器使用默认构造函数创建新书对象。如果在默认构造函数中抛出异常,您可以看到调用构造函数的整个堆栈(或使用调试器),只需将try{throw new exception();}catch(exception e){e.printStackTrace();}添加到构造函数中并运行它again@outofBounds或者您可以只使用static
Thread.dumpStack()
@yshavit-thx不知道这一点。它更简单、更干净。你能解释一下为什么最后只有4张照片,而不是6张或8张。。有3个字段和一个标识。你能详细说明一下构造函数是在什么时候调用的吗?。很抱歉,我不记得JPA中使用的确切流程:)如果你感兴趣,你应该使用Thread.dumpStack(),就像Havit建议的那样。但请注意,打印是在构造函数中使用的,而不是在字段的getter/setter中使用的。。。因为有两本书,我可能认为每个实体的构造函数被JPA调用了两次。你能解释一下为什么最后只有4个打印,而不是6个或8个。。有3个字段和一个标识。你能详细说明一下构造函数是在什么时候调用的吗?。很抱歉,我不记得JPA中使用的确切流程:)如果你感兴趣,你应该使用Thread.dumpStack(),就像Havit建议的那样。但请注意,打印是在构造函数中使用的,而不是在字段的getter/setter中使用的。。。因为有两本书,我可能认为每个实体的构造函数都会被JPA调用两次。