2

以下代码用于在 mysql 数据库中插入删除和更新数据表名是学生,我使用这个学生表创建了一个视图如果可能的话我可以在这个视图上执行插入更新删除操作建议我如何

此代码用于检索对象

 package roseindia.tutorial.hibernate;
    import org.hibernate.Transaction;

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;


    import org.hibernate.cfg.Configuration;
    public class FirstExam {
    public static void main(String[] args)
    {
    Session session = null;

    try{


    SessionFactory sessionFactory =newConfiguration().configure().buildSessionFactory();
    session =sessionFactory.openSession();

    Transaction transacton =session.beginTransaction();


    System.out.println("Inserting Record");
    Contact contact = new Contact();

    //contact.setId(1);
    contact.setFirstName("Vikas");
    contact.setLastName("Kumar");
    contact.setEmail("vikas@gmail.com");
    session.save(contact);
    //session.update(contact);
    //session.delete(contact);
    transacton.commit();
    System.out.println("Done");

    }
    catch(Exception e)
    {
    System.out.println(e.getMessage());
    }
    finally
    {
    // Actual contact insertion will happen at this step
    session.flush();
    session.close();

    }

    }
    }

持久性类的代码

    package roseindia.tutorial.hibernate;


    public class Contact {
        private String firstName;
        private String lastName;
        private String email;
        private int id;


        public String getEmail() {
            return email;
        }


        public String getFirstName() {
            return firstName;
        }


        public String getLastName() {
            return lastName;
        }


        public void setEmail(String string) {
            email = string;
        }


        public void setFirstName(String string) {
            firstName = string;
        }


        public void setLastName(String string) {
            lastName = string;
        }


        public int getId() {
            return id;
        }


        public void setId(int i) {
            id = i;
        }

    }

映射类

<hibernate-mapping>
  <class name="roseindia.tutorial.hibernate.Contact" table="STUDENT">
       <id name="id" type="int" column="ID" >
         <generator class="increment"/>
      </id>

      <property name="firstName">
         <column name="FIRSTNAME" />
      </property>
      <property name="lastName">
        <column name="LASTNAME"/>
      </property>
      <property name="email">
        <column name="EMAIL"/>
      </property>
   </class>
</hibernate-mapping>

配置类

<hibernate-configuration>
<session-factory>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://192.168.10.161/testdb</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">root</property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- Mapping files -->
      <mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4

0 回答 0