我正在尝试使用 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
我不明白,为什么有这么多默认构造函数调用,而我却没有一个......?
有人可以解释一下我所拥有的样本中的流程吗?