Hibernate 4.3+MySql demo

jar

entity annotation configuration

package com.demo.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


@Entity(name="CodeType")
public class CodeType implements java.io.Serializable {

 // Fields

 @Id
 @GeneratedValue
 @Column(name = "id")
 private Integer id;
 @Column
 private String typeName;

 // Constructors

 /** default constructor */
 public CodeType() {
 }

 /** full constructor */
 public CodeType(String typeName) {
  this.typeName = typeName;
 }

 // Property accessors

 public Integer getId() {
  return this.id;
 }

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

 public String getTypeName() {
  return this.typeName;
 }

 public void setTypeName(String typeName) {
  this.typeName = typeName;
 }

}
View Code

or xml configuration

<?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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.demo.entity.CodeType" table="codetype">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="typeName" type="java.lang.String">
            <column name="typeName" length="2000000000" />
        </property>
    </class>
</hibernate-mapping>
View Code

 

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
   <property name="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
      com.mysql.jdbc.Driver
   </property>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://localhost/codesystem
   </property>
   <property name="hibernate.connection.username">
      root
   </property>
   <property name="hibernate.connection.password">
      12345
   </property>

   <!-- List of XML mapping files -->
<!--    <mapping resource="com/demo/entity/CodeType.hbm.xml"/> -->
<mapping class="com.demo.entity.CodeType"/>

</session-factory>
</hibernate-configuration>
View Code

 

HibernateUtil.java

package com.demo.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;


public class HibernateUtil {

    public static SessionFactory getSessionFactory() {
        SessionFactory sf = null;

        try {
            Configuration cfg = new Configuration();

            cfg.configure();

            @SuppressWarnings("deprecation")
            ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
                    cfg.getProperties()).build();

            sf = cfg.buildSessionFactory(sr);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return sf;
    }

}
View Code

 

dao

package com.demo.dao.impl;


import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.demo.dao.CodeTypeDao;
import com.demo.entity.CodeType;


public class CodeTypeDaoImpl implements CodeTypeDao {
    public List<CodeType> getCondeTypeList()
    {
        
        Session session =  HibernateUtil.getSessionFactory().openSession();
        List<CodeType> list= session.createQuery("from CodeType").list();
        //return null;
        return list;
    }
    public void save(){
        Session session =  HibernateUtil.getSessionFactory().openSession();
        Transaction  t =session.getTransaction();
        t.begin();
        session.save(new CodeType("C#"));
        t.commit();
        session.flush();
        session.close();
    }
    public static void main(String[] args) {
        CodeTypeDao dao = new CodeTypeDaoImpl();
        //dao.save();
        System.out.println(dao.getCondeTypeList().size());
        
    }
}
View Code

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。