PostgreSQL
 sql >> Teknologi Basis Data >  >> RDS >> PostgreSQL

Hibernate + PostgreSQL + Jenis Alamat Jaringan (inet, cdir)

Saya hanya memecahkan masalah seperti Anda. Ada sedikit info tentang masalah ini. Seperti yang dikatakan @ms03...cara yang lebih baik adalah membuat tipe baru dan kelas yang mengimplementasikan tipe ini dan menangani objek Java untuk mengatur/mendapatkan database.

Pertama pada entitas Anda, Anda harus mendeklarasikan tipe untuk params Anda.

@Entity
@Table(name="user_app")
@TypeDefs(value={@TypeDef(name="convertInet",typeClass=PgInetType.class),
         @TypeDef(name="convertMacaddr",typeClass=PgMacaddrType.class)})
public class User implements Serializable {

    //some parameters

  @Type(type="convertMacaddr")
  private PgMacaddr mac;

  @Type(type="convertInet")
  private PgInet ip;

     //getters and setters, equals, hashcode, toString
  }

Kedua, buat objek baru yang ditangani untuk java, objek tersebut harus memiliki konstruk default, konstruk untuk menangani data dari DB dan menjadi Seriarizable.

public class PgInet implements Serializable {

private static final long serialVersionUID = 1L;

private String address;

public PgInet(String address){
    this.address=address;
}

public PgInet(){
    this.address=null;
}

    //Getters,setters,hashcode, equal and toString
 }

Dan langkah terakhir Anda harus membuat jenis kustom

public class PgInetType implements UserType{

@Override
public int[] sqlTypes() {

            //Because inet,macaddr,cdir...and unkwon type for java, yo must
            // define Types.OTHER
    return new int[] { Types.OTHER };
}

@Override
public Class returnedClass() {

            //Object created to be handled for java
    return PgInet.class;
}

@Override
public boolean equals(Object x, Object y) throws HibernateException {
    return ObjectUtils.nullSafeEquals(x, y);
}

@Override
public int hashCode(Object x) throws HibernateException {
    if(x!=null)
        return x.hashCode();
    else
        return 0;
}

@Override
public Object nullSafeGet(ResultSet rs, String[] names,
        SessionImplementor session, Object owner)
        throws HibernateException, SQLException {

            //Translation from DB to Java
    PgInet address=null;

    String ip=rs.getString(names[0]);

    if(ip!=null){
        address=new PgInet(ip);
    }

    return address;
}

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index,
        SessionImplementor session) throws HibernateException, SQLException {

    //Translation from java to DB

    if(value==null){
        st.setNull(index, Types.VARCHAR);
    }else{

                //As inet,macaddr,cdir...are new types object on Postgresql you must
                //create the specific postgresql object and to insert it

                //I created 2 new cast on postgresql: inet As varchar, varchar AS inet
                //but I think it's not neccesary because macaddr type works fine without
                //postgresl casting

        st.setObject(index, getInet(value, st.getConnection()));
    }

}

private Object getInet(Object value, Connection connection) {

           //Expected object on postgresql

       Object tempInet = null;
       ClassLoader connectionClassLoader = connection.getClass().getClassLoader();

       try {

           //Class which will create the postgresql

           Class aPGObjectClass =connectionClassLoader.loadClass("org.postgresql.util.PGobject");
           Constructor ct = aPGObjectClass.getConstructor(null);
           try {
            tempInet = ct.newInstance(null);
        } catch (InstantiationException | IllegalAccessException
                | IllegalArgumentException | InvocationTargetException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

           Method setTypeMethod = aPGObjectClass.getMethod("setType", new Class[]{String.class});
           try {

               //Setting postgresql type, inet in this case

            setTypeMethod.invoke(tempInet, new Object[]{"inet"});
        } catch (IllegalAccessException | IllegalArgumentException
                | InvocationTargetException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

           Method setValueMethod = aPGObjectClass.getMethod("setValue", new Class[]{String.class});
           try {
            setValueMethod.invoke(tempInet, new Object[]{value.toString()});
        } catch (IllegalAccessException | IllegalArgumentException
                | InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

       } catch (ClassNotFoundException e) {

       } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

       return tempInet;
   }

@Override
public Object deepCopy(Object value) throws HibernateException {
    if(value==null)
        return null;
    else{
        PgInet PgInetNew=new PgInet();
        PgInet PgInetOriginal=(PgInet)value;

        PgInetNew.setAddress(PgInetOriginal.getAddress());

        return PgInetNew;
    }
}

@Override
public boolean isMutable() {
    return false;
}

@Override
public Serializable disassemble(Object value) throws HibernateException {
     Object  deepCopy=deepCopy(value);

      if(!(deepCopy instanceof Serializable))
       return (Serializable)deepCopy;

      return null;
}

@Override
public Object assemble(Serializable cached, Object owner)
        throws HibernateException {
    return deepCopy(cached);
}

@Override
public Object replace(Object original, Object target, Object owner)
        throws HibernateException {
    return deepCopy(original);
}

 }

Cara ini bekerja dengan baik untuk saya ketika saya memasukkan atau mendapatkan baris dari DB.



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Postgresql :Koneksi ditolak. Periksa apakah nama host dan port sudah benar dan postmaster menerima koneksi TCP/IP

  2. Tantangan Menskalakan Database PostgreSQL Moodle

  3. Bagaimana cara mengupdate semua kolom dengan INSERT...ON CONFLICT...?

  4. Haruskah saya menentukan INDEX dan UNIQUE INDEX?

  5. Dapatkan Id dari INSERT bersyarat