首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

介绍Hibernate使用UserType-ClobStringType

2013-01-25 
介绍Hibernate使用UserType--ClobStringType这里介绍Hibernate使用UserType,UserType就是用户自定义类型,

介绍Hibernate使用UserType--ClobStringType

    这里介绍Hibernate使用UserType,UserType就是用户自定义类型,这里的类型指的是除了Hibernate定义的那些类型之外的用户自己定义的。

    UserType就是用户自定义类型,这里的类型指的是除了Hibernate定义的那些类型之外的用户自己定义的。

    一个实现Hibernate使用UserType接口的 email 类如下(里面的每个方法都是必须实现的接口方法,许多都可以复制粘贴的,不复杂):

      /*
      * $HeadURL$
      * Copyright (c) 2003-2007 Untangle, Inc.
      *
      * This program is free software; you can redistribute it and/or modify
      * it under the terms of the GNU General Public License, version 2,
      * as published by the Free Software Foundation.
      *
      * This program is distributed in the hope that it will be useful, but
      * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty of
      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
      * NONINFRINGEMENT.? See the GNU General Public License for more details.
      *
      * You should have received a copy of the GNU General Public License
      * along with this program; if not, write to the Free Software
      * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
      */

      package com.untangle.uvm.type;

      import java.io.Serializable;
      import java.net.InetAddress;
      import java.net.UnknownHostException;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Types;

      import org.hibernate.HibernateException;
      import org.hibernate.usertype.UserType;

      /**
      * Hibernate <code>UserType</code> for persisting
      * <code>InetAddress</code> objects.
      *
      * @author <a href="mailto:amread@untangle.com">Aaron Read</a>
      * @version 1.0
      */
      public class InetAddressUserType implements UserType
      {
      ??? private static final int[] SQL_TYPES = { Types.VARCHAR };

      ??? public int[] sqlTypes() { return SQL_TYPES; }//该类型对应的?sql?类型??
      ??? public Class<InetAddress> returnedClass() { return InetAddress.class; }

      ??? public boolean equals(Object x, Object y) //equals方法,这个就不用多说了吧,肯定是要用户自定义的
      ??? {
      ??????? if (x == y) { return true; }
      ??????? if (x == null || y == null) { return false; }
      ??????? return x.equals(y);
      ??? }

      ??? public Object deepCopy(Object value) { return value; } //?完全复制的方法,由于是用户自己定义的类型//所以hibernate并不知道要如何来复制这个类,需要用户自己定义??
      ??? public boolean isMutable() { return false; }?//?表示本类型实例是否可变,一般是不可变 ?
    1. /**?*//**//* ?这才是重头戏!nullSafeGet?和nullSafeSet?是核心所在,对数据的后期处理都在这两个方法里面 ?
    2. nullSafeGet?是读取的方法 ?owner?目前没用到过。 ?
    3. names?是对应的数据库列名,UserType是可以对应多个列的 ?*/
      ??? public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
      ??????? throws HibernateException, SQLException
      ??? {
      ??????? String name = rs.getString(names[0]);

      ??????? if (rs.wasNull()) { return null; }

      ??????? try {
      ??????????? return InetAddress.getByName(name);
      ??????? } catch (UnknownHostException exn) {
      ??????????? throw new HibernateException(exn);
      ??????? }
      ??? }
    1. /**//* ?保存的方法 ?
    2. index?是那个?PreparedStatement?的参数序号,一般来说不用管直接往下传 ?value?就是要保存的数据,在这边是一个保存着?email?列表的?List ?
    3. */ ?
      ??? public void nullSafeSet(PreparedStatement ps, Object v, int i)
      ??????? throws HibernateException, SQLException
      ??? {
      ??????? if (null == v) {
      ??????????? ps.setNull(i, Types.VARCHAR);
      ??????? } else {
      ??????????? InetAddress addr = (InetAddress)v;
      ??????????? ps.setString(i, addr.getHostAddress());
      ??????? }
      ??? }

      ??? public Object replace(Object original, Object target, Object owner)
      ??? {
      ??????? return original;
      ??? }

      ??? public Object assemble(Serializable cached, Object owner)
      ??? {
      ??????? return deepCopy(cached);
      ??? }

      ??? public Serializable disassemble(Object value)
      ??? {
      ??????? return (Serializable)deepCopy(value);
      ??? }

      ??? public int hashCode(Object x)
      ??? {
      ??????? return x.hashCode();
      ??? }
      }

    ?

    ?

    下来是对应pojo类,这个类用了相应的注解

    package com.untangle.uvm.logging;

    import java.net.InetAddress;
    import java.util.Date;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;
    import javax.persistence.Transient;

    import org.hibernate.annotations.Type;

    import com.untangle.uvm.logging.PipelineEvent;
    import com.untangle.uvm.logging.SyslogBuilder;
    import com.untangle.uvm.logging.SyslogPriority;
    import com.untangle.uvm.node.PipelineEndpoints;

    /**
    ?* Log event from the denormalized reports.n_cpd_block_events reports table
    ?*
    ?* @author Sebastien Delafond
    ?* @version 1.0
    ?*/
    @Entity
    ??? @org.hibernate.annotations.Entity(mutable=false)
    ??? @Table(name="n_cpd_block_events", schema="reports")
    ??? @SuppressWarnings("serial")
    ??? public class CpdBlockEventsFromReports extends LogEvent
    ??? {
    ??????? private InetAddress clientAddress;
    ??????? private InetAddress serverAddress;
    ??????? private Integer clientPort;
    ??????? private Integer serverPort;
    ??????? private Integer clientIntf;
    ??????? private Integer proto;

    ??????? @Column(name="client_address")
    ??????? @Type(type="com.untangle.uvm.type.InetAddressUserType")//自定义的usertype
    ??????? public InetAddress getClientAddress() { return clientAddress; }
    ??????? public void setClientAddress(InetAddress clientAddress) { this.clientAddress = clientAddress; }

    ??????? @Column(name="server_address")
    ??????? @Type(type="com.untangle.uvm.type.InetAddressUserType")
    ??????? public InetAddress getServerAddress() { return serverAddress; }
    ??????? public void setServerAddress(InetAddress serverAddress) { this.serverAddress = serverAddress; }

    ??????? @Column(name="client_port")
    ??????? public Integer getClientPort() { return clientPort; }
    ??????? public void setClientPort(Integer clientPort) { this.clientPort = clientPort; }

    ??????? @Column(name="server_port")
    ??????? public Integer getServerPort() { return serverPort; }
    ??????? public void setServerPort(Integer serverPort) { this.serverPort = serverPort; }

    ??????? @Column(name="proto")
    ??????? public Integer getProto() { return proto; }
    ??????? public void setProto(Integer proto) { this.proto = proto; }

    ??????? @Column(name="client_intf")
    ??????? public Integer getClientIntf() { return clientIntf; }
    ??????? public void setClientIntf(Integer clientIntf) { this.clientIntf = clientIntf; }

    ??????? public void appendSyslog(SyslogBuilder sb) // FIXME: not called for now
    ??????? {
    ??????? }

    ??????? @Transient
    ??????? public String getSyslogId()
    ??????? {
    ??????????? return ""; // FIMXE ?
    ??????? }

    ??????? @Transient
    ??????? public SyslogPriority getSyslogPriority()
    ??????? {
    ??????????? // FIXME
    ??????????? return SyslogPriority.INFORMATIONAL;
    ??????? }

    ??? }

热点排行