脱离容器的EJB

 

 

 需要引入的包 hibernate - annotations,hibernate,hibernate - entitymanager 这3个包及附带的包

 

配置文件META-INF/persistence.xml

 

<? xml version = " 1.0 " ?>
< persistence xmlns = " http://java.sun.com/xml/ns/persistence " >
    
< persistence - unit name = " example " >
        
< provider > org.hibernate.ejb.HibernatePersistence </ provider >
        
< class > com.foshanshop.ejb3.bean.Person </ class >
        
< class > com.foshanshop.ejb3.bean.Order </ class >
        
< class > com.foshanshop.ejb3.bean.OrderItem </ class >
        
< class > com.foshanshop.ejb3.bean.Teacher </ class >
        
< class > com.foshanshop.ejb3.bean.Student </ class >
        
< properties >
            
< property name = " hibernate.connection.driver_class "
                value
= " com.microsoft.sqlserver.jdbc.SQLServerDriver "   />
            
< property name = " hibernate.connection.url "
                value
= " jdbc:sqlserver://127.0.0.1:1433; DatabaseName=EJB "   />
            
< property name = " hibernate.dialect "
                value
= " org.hibernate.dialect.SQLServerDialect "   />
            
< property name = " hibernate.connection.username "
                value
= " ls "   />
            
< property name = " hibernate.connection.password "
                value
= " 123456 "   />
            
< property name = " hibernate.max_fetch_depth "  value = " 3 "   />
            
< property name = " hibernate.hbm2ddl.auto "  value = " create "   />
            
< property name = " hibernate.show_sql "  value = " true "   />
        
</ properties >
    
</ persistence - unit >
</ persistence >

持久类

 

 

package  com.foshanshop.ejb3.bean;

import  java.io.Serializable;
import  java.util.Date;
import  javax.persistence.Column;
import  javax.persistence.Entity;
import  javax.persistence.GeneratedValue;
import  javax.persistence.Id;
import  javax.persistence.Table;
import  javax.persistence.Temporal;
import  javax.persistence.TemporalType;
import  javax.persistence.GenerationType;

@Entity


public   class  Person  implements  Serializable  {
    @Id
    @GeneratedValue
    
private Integer personid;

    @Column(nullable 
= false, length = 32)
    
private String name;

    
private boolean sex;

    
private Short age;
 
    
private Date birthday;
    


    
public Integer getPersonid() {
        
return personid;
    }


    
public void setPersonid(Integer personid) {
        
this.personid = personid;
    }


    
public String getName() {
        
return name;
    }


    
public void setName(String name) {
        
this.name = name;
    }


    
// @Column(nullable=false)
    public boolean getSex() {
        
return sex;
    }


    
public void setSex(boolean sex) {
        
this.sex = sex;
    }


    @Column(nullable 
= false)
    
public Short getAge() {
        
return age;
    }


    
public void setAge(Short age) {
        
this.age = age;
    }


    
    
public Date getBirthday() {
        
return birthday;
    }


    
public void setBirthday(Date birthday) {
        
this.birthday = birthday;
    }



}

 

测试类:

 

 

package com.foshanshop.ejb3;

import java.text.SimpleDateFormat;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import com.foshanshop.ejb3.bean.Person;

public   class  Example  {
    EntityManagerFactory emf;

    
public Example() {
        emf 
= Persistence.createEntityManagerFactory("example");//example 为配置文件中指定
        
    }


    
public void exercise() throws Exception {
        Person p 
= new Person();
        p.setName(
"John Doef1df2");
        p.setAge((
short25);
        p.setSex(
false);
        SimpleDateFormat sf 
= new SimpleDateFormat("yyyy-MM-dd");
        p.setBirthday(sf.parse(
"1990-02-01"));
        p.setD(
"abc");
        
        savePerson(p);
      
        Person py 
= findByName("John Doef1df2");
        System.
out.println(py);
        emf.close();
    }


    
private Person findByName(String name) {
        EntityManager em 
= emf.createEntityManager();
        Query q 
= em.createQuery("select person from Person as person where name=:param");
        
        q.setParameter(
"param", name);
        Person p 
= (Person) q.getSingleResult();
        em.close();
        
return p;
    }




    
private Person findById(Long id) {
        EntityManager em 
= emf.createEntityManager();
        Person p 
= em.find(Person.class, id);
        em.close();
        
return p;
    }


    
private int savePerson(final Person p) {
        EntityManager em 
= emf.createEntityManager();
        EntityTransaction tx 
= em.getTransaction();
        tx.begin();
        Person pp 
= em.merge(p);
        tx.commit();
        em.close();
        
return pp.getPersonid();
    }




    
public static void main(String[] args) throws Exception {
        Example example 
= new Example();
        example.exercise();
        
try {
            Thread.sleep(
30000);
        }

        
catch (InterruptedException ex) {
        }

    }

}

你可能感兴趣的:(Hibernate,properties,ejb,Class,import,Annotations)