如何在Hibernate

时间:2015-06-26 02:55:35

标签: java xml hibernate

这是我的LOGIN1.java类

import java.io.Serializable;
public class LOGIN1 implements Serializable
{
    /**
 * 
 */
  private static final long serialVersionUID = 1L;
    private int id;
    private String Email;
    private String Password;
    private Registration registration;



    public Registration getRegistration() {
        return registration;
    }

    public void setRegistration(Registration registration) {
        this.registration = registration;
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEmail() {
        return Email;
    }
    public void setEmail(String email) {
        Email = email;
    }
    public String getPassword() {
        return Password;
    }
    public void setPassword(String password) {
        Password = password;
    }

    }

这是我的Registration.java     import java.io.Serializable;

public class Registration implements Serializable
{
/**
 * 
 */
private static final long serialVersionUID = 1L;

private int id;
private String contact;
private String Name;
private String amount;
private LOGIN1 login1;



public LOGIN1 getLogin1() {
    return login1;
}
public void setLogin1(LOGIN1 login1) {
    this.login1 = login1;
}
public int getId()
{
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getContact() {
    return contact;
}
public void setContact(String contact) {
    this.contact = contact;
}
public String getName() {
    return Name;
}
public void setName(String name) {
    Name = name;
}
public String getAmount() {
    return amount;
}
public void setAmount(String amount) {
    this.amount = amount;
}

}

这是我的LOGIN1.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="LOGIN1" table="LOGIN1">

<id name="id" column="id" type="integer">

<generator class="increment"/>

</id>

<property name="Email" column="EMAIL" type="string"></property>

<property name="Password" column="Password" type="string"></property>

<one-to-one name="registration" class="Registration" cascade="save-update"></one-to-one>
</class>                              

</hibernate-mapping>

这是我的Registration.hbm.xml                    

<id name="id" column="id" type="integer">

        <generator class="foreign">
            <param name="property">rg</param>
            </generator>
        </id>


<one-to-one name="login1" class="LOGIN1" constrained="true"></one-to-one>

<property name="Name" column="Name" type="string"></property>

<property name="contact" column="contct" type="string"></property>

<property name="amount" column="iamt" type="string"></property>

</class>

</hibernate-mapping>

这是我的hibernate.cfg

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration> 
<session-factory>                         
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=HiberNate</property>
<property name="hibernate.connection.username">aaa</property>
<property name="hibernate.connection.password">aaa</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="show_sql">true</property>
<mapping resource="LOGIN1.hbm.xml"></mapping>
<mapping resource="Registration.hbm.xml"></mapping>

</session-factory>

</hibernate-configuration>

这是我的Hiberservlets     import java.io.IOException;     import java.io.PrintWriter;     import javax.servlet.ServletException;     import javax.servlet.annotation.WebServlet;     import javax.servlet.http.HttpServlet;     import javax.servlet.http.HttpServletRequest;     import javax.servlet.http.HttpServletResponse;     import org.hibernate.Criteria;     import org.hibernate.HibernateException;     import org.hibernate.Session;     import org.hibernate.SessionFactory;     import org.hibernate.Transaction;     import org.hibernate.boot.registry.StandardServiceRegistryBuilder;     import org.hibernate.cfg.Configuration;     import org.hibernate.criterion.Restrictions;     import org.hibernate.service.ServiceRegistry;     import org.hibernate.service.ServiceRegistryBuilder;     @WebServlet(&#34; / HiberServlet&#34)     公共类HiberServlet扩展了HttpServlet {     private static final long serialVersionUID = 1L;

public HiberServlet() {
}


@SuppressWarnings("deprecation")
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try 
    {
     out.print("Hello");      
     String a,b,c,d,e;

    a=request.getParameter("cnm");
    b=request.getParameter("cntno");
    c=request.getParameter("amt");
    d=request.getParameter("eml");
    e=request.getParameter("pwd");

    Session s=HibernateUtil.openSession();
    Transaction tx = s.beginTransaction();


    try {

    LOGIN1 ln=new LOGIN1();
    ln.setEmail(d);
    ln.setPassword(e);
    s.save(ln);

    Criteria cr=s.createCriteria(LOGIN1.class);
    cr.add(Restrictions.eq("Email",d));
    cr.add(Restrictions.eq("Password",e));    
    int i=ln.getId();

  Registration rg=new Registration();
  rg.setId(i);
  rg.setName(a);
  rg.setContact(b);
  rg.setAmount(c);

  s.save(rg);

  tx.commit();

         }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    if(s!=null)
    {
     s.flush();                    
     s.close();   
    }     
    }
    catch(HibernateException e)
    {
        System.out.print(e.getMessage());

    }

}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

}

我正在尝试这个,但它的Give错误就像     显示java.lang.NullPointerException     org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:650)     org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:4736)     org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:96)     org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:118)     org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)     org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)     org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)     org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)     org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)     org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)     org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)     org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)     HiberServlet.doGet(HiberServlet.java:77)     javax.servlet.http.HttpServlet.service(HttpServlet.java:621)     javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

我正在使用eclipse。这段代码中存在什么问题。

1 个答案:

答案 0 :(得分:0)

org.hibernate.PropertyNotFoundException: field [LOGIN1] not found on Registration

表示您的地图与您的班级不符。类中可能没有LOGIN1属性。如果它完全存在,则可能用小写字母称为login1