JPA

 JPA是什么?JPA是数据库的访问,类似JDBC,Hibernate,但是更方便,开发更容易

JPA使得系统可以很容易的在不同数据库间一直(貌似Hibernate也可以),但是JPA的数据库实体功能比Hibernate更强大。

 

JPA应用我觉得包含3个部分:

1,Entity,即实体,JPA将数据库表看出独立的实体类。数据库表间的关联也可以通过实体类的注释实现。

如:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dcampus.snms.event.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 *
 */
@Entity
@Table(name = "Event")
public class Event implements Serializable {

    private int eventId;
    private String alarmName;
    private int enable;
    private Category category;
    private EventType eventType;
    private EventName eventName;
    private Severity severity;

    @Id
    @Column(name = "eventId")
    public int getEventId() {
        return eventId;
    }

    public void setEventId(int eventId) {
        this.eventId = eventId;
    }

    @Column(name = "alarmName")
    public String getAlarmName() {
        return alarmName;
    }

    public void setAlarmName(String alarmName) {
        this.alarmName = alarmName;
    }
    @ManyToOne
    @JoinColumn(name = "severityId")
    public Severity getSeverity() {
        return severity;
    }

    public void setSeverity(Severity severity) {
        this.severity = severity;
    }

    @Column(name = "enable")
    public int getEnable() {
        return enable;
    }

    public void setEnable(int enable) {
        this.enable = enable;
    }

    @ManyToOne
    @JoinColumn(name = "eventTypeId")
    public EventType getEventType() {
        return eventType;
    }

    public void setEventType(EventType eventType) {
        this.eventType = eventType;
    }

    @ManyToOne
    @JoinColumn(name = "eventNameId")
    public EventName getEventName() {
        return eventName;
    }

    public void setEventName(EventName eventName) {
        this.eventName = eventName;
    }

    @ManyToOne
    @JoinColumn(name = "categoryId")
    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }
}

 这个实体类涉及到了好几张表,包括Event,Category,Severity等

2,Entity配置文件,只有进行了配置,实体才能和数据库对应,才能实现应用,给一个应用的例子

persistence.xml:一般在NetBeans里面,persistence.xml包含在目录META-INF中,和类包在同一层。

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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_1_0.xsd">
  <persistence-unit name="event" transaction-type="RESOURCE_LOCAL">
    <provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
    <class>com.dcampus.snms.event.entity.EventLog</class>
    <class>com.dcampus.snms.event.entity.Event</class>
    <class>com.dcampus.snms.event.entity.Category</class>
..........
    <properties>
      <property name="toplink.logging.level" value="FINE"/>
      <property name="toplink.jdbc.url" value="jdbc:mysql://192.168.222.5:3306/xxxx?autoReconnect=true"/>
      <property name="toplink.jdbc.user" value="aaaa"/>
      <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="toplink.jdbc.password" value="bbbb"/>
    </properties>
  </persistence-unit>
</persistence>

 

3,实体的应用,类EventJPA

private static EventJPA jpa;
    private EntityManagerFactory emf;
    //private static EntityManager em;
    private String pun = "event";

    public EventJPA() {
        init();
    }

    private void init() {
        emf = Persistence.createEntityManagerFactory(pun);
    }

    public static EventJPA getInstance() {
        if (jpa == null) {
            jpa = new EventJPA();
        }
        return jpa;
    }

public ArrayList<EventLog> getLast300EventLogs() {
        ArrayList<EventLog> elogs = new ArrayList<EventLog>();
        if (!emf.isOpen()) {
            init();
        }
        EntityManager em = emf.createEntityManager();
        long maxId = this.getMaxEventLogId().longValue();
        Query query = em.createQuery("select e from EventLog e where e.eventLogId>=:beginId");
        query.setParameter("beginId", maxId - 300);
        List results = query.getResultList();
        em.close();
        for (Object o : results) {
            EventLog el = (EventLog) o;
            elogs.add(el);
        }
        return elogs;
    }

 

4,我用的这个JPA采用的是Oracle的toplink,其实JPA有很多的开源包可以用,有不少选择

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值