如何使用CDI EntityManager

时间:2016-06-03 02:35:03

标签: java hibernate cdi

我有一个使用Hibernate的Maven JSF项目,Wildfly,CDI正在照顾实体经理'注入,我使用mySql和JTA与数据源设置来访问数据库。

我需要创建一个带有main的java,它定期访问数据库并搜索不具有纬度和经度的地址。此java将捕获谷歌地图中的新纬度和经度,然后将这些值保存在名为RegisteredAddress的表中。

问题是我需要在这个新的java文件中访问RegisteredAddressDAO,但是EntityManager是由CDI注入的。由于这个java文件将单独运行(不会通过请求触发),如何在此java文件中使用此DAO?

standalone-ha.xml - 数据源设置

<datasource jndi-name="java:/dbmap" pool-name="dbmap" enabled="true" use-java-context="true">
                <connection-url>jdbc:mysql://localhost:3306/dbmap</connection-url>
                <driver>com.mysql</driver>
                <pool>
                    <min-pool-size>10</min-pool-size>
                    <max-pool-size>100</max-pool-size>
                    <prefill>true</prefill>
                </pool>
                <security>
                    <user-name>root</user-name>
                    <password>a123</password>
                </security>
            </datasource>

的persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">

    <persistence-unit name="dbmap" transaction-type="JTA">

        <jta-data-source>java:/dbmap</jta-data-source>


        <class>com.map.model.RegisteredAddress</class>


        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        </properties>
    </persistence-unit>
</persistence>

RegisteredAddress.java

@Entity
public class RegisteredAddress implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer idRegisteredAddress;
    @ManyToOne
    private City city;
    private String zipCode;
    private String address;
    private String neighbourhood;
    private boolean isOfficial;
    private double latitude;
    private double longitude;

    public Integer getIdRegisteredAddress() {
        return idRegisteredAddress;
    }
    public void setIdRegisteredAddress(Integer idRegisteredAddress) {
        this.idRegisteredAddress = idRegisteredAddress;
    }
    public City getCity() {
        return city;
    }
    public void setCity(City city) {
        this.city = city;
    }
    public String getZipCode() {
        return zipCode;
    }
    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getNeighbourhood() {
        return neighbourhood;
    }
    public void setNeighbourhood(String neighbourhood) {
        this.neighbourhood = neighbourhood;
    }
    public boolean isOfficial() {
        return isOfficial;
    }
    public void setOfficial(boolean isOfficial) {
        this.isOfficial = isOfficial;
    }
    public double getLatitude() {
        return latitude;
    }
    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }
    public double getLongitude() {
        return longitude;
    }
    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
}

RegisteredAddressDao

@SuppressWarnings("serial")
@Stateless
public class RegisteredAddressDao implements Serializable{
    @PersistenceContext
    EntityManager em;

    private DAO<RegisteredAddress> dao;

    @PostConstruct
    void init() {
        this.dao = new DAO<RegisteredAddress>(this.em, RegisteredAddress.class);
    }

    public void insert(RegisteredAddress o){
        dao.insert(o);
    }

    public void update(RegisteredAddress o){
        dao.update(o);
    }

    public void remove(RegisteredAddress o){
        this.em.remove(o);
    }

    public RegisteredAddress searchById(Integer ido){
        return dao.searchById(ido);
    }

    public List<RegisteredAddress> listAll(){
        return dao.listAll();
    }
}

DAO

@SuppressWarnings("serial")
public class DAO<T> implements Serializable {

    private final Class<T> classe;
    private EntityManager em;

    public DAO(EntityManager manager, Class<T> classe) {
        this.em = manager;
        this.classe = classe;
    }

    public void insert(T t) {
        em.persist(t);
    }

    public void remove(T t) {
        em.remove(em.merge(t));
    }

    public void update(T t) {
        em.merge(t);
    }

    public List<T> listAll() {
        CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(classe);
        query.select(query.from(classe));

        List<T> list = em.createQuery(query).getResultList();

        return list;
    }

    public T searchById(Integer id) {
        T instance = em.find(classe, id);
        return instance;
    }

    public int countAll() {
        long result = (Long) em.createQuery("select count(n) from livro n")
                .getSingleResult();

        return (int) result;
    }

    public List<T> listAllPaged(int firstResult, int maxResults) {
        CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(classe);
        query.select(query.from(classe));

        List<T> list = em.createQuery(query).setFirstResult(firstResult)
                .setMaxResults(maxResults).getResultList();

        return list;
    }
}

0 个答案:

没有答案