public static void main(String[]arg)
{
Session session = null;
try{
@SuppressWarnings("deprecation")
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Contact contact = new Contact();
contact.setID(3);
contact.setFirstName("Professor");
contact.setLastName("Katagawa");
contact.setEmail("Bradsis@yahoo.com");
session.save(contact);
System.out.println("Done");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
session.flush();
session.close();
}
}
我总是在 session.flush() 中得到一个 NullPointer 异常。我已经有一个类 Contact 定义如下,
public class Contact
{
private String firstName;
private String lastName;
private String email;
private int id;
public String getEmail()
{
return email;
}
public String getLastName()
{
return lastName;
}
public String getFirstName()
{
return firstName;
}
public int getID()
{
return id;
}
public void setID(int ID)
{ id=ID; }
public void setEmail(String e)
{
email=e;
}
public void setFirstName(String fn)
{
firstName=fn;
}
public void setLastName(String ln)
{
lastName=ln;
}
}
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
main 方法中的行
被报告为已弃用。我也有一个正确的映射文件。非常感谢您的帮助。