2014-01-30 76 views
1

我有兩個實體名爲「Padre」和「Figlio」 Padre是所有者,Figlio是子 Padre的主鍵有兩列PADREK1和PADREK2,兩個CHAR(20) FIGLIO的主鍵有四列: PADREPADREK1(國外)CHAR(20) PADREPADREK2(國外)CHAR(20) FIGLIOK1 CHAR(20) FIGLIOK2 CHAR(20)休眠一對多的單向關係使用多列的SQL類型CHAR

這是我的實體類代碼

 
    public class PadreBean extends BaseEntityBean3 implements Serializable 
    { 
     @EmbeddedId 
     PadrePK iPadrePK; 
     @Column(name = "DESCRIPTION") 
     private String iDescription; 
     @Column(name = "ABSUNIQUEID") 
     private long iABSUniqueId; 
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
     @JoinColumns(
      { 
      @JoinColumn(name = "ABSOUTQUEUENAME", nullable = true) 
      }) 
     private ABSOutQueueBean iAbsOutQueueBean; 
     @OneToMany(fetch = FetchType.LAZY) 
     @JoinColumns({ 
      @JoinColumn(name = "PADREPADREK1", referencedColumnName = "PADREK1"), 
      @JoinColumn(name = "PADREPADREK2", referencedColumnName = "PADREK2") 
     }) 
     List iFiglioBeans; 
     ...... 

iFiglioBeans是孩子

收集的問題是: 如果兩個策略平臺的主鍵列所有正在使用20個字符填滿,一切運作良好; id padre的主鍵列未被完全填充Hibernate確實加載了集合 ,但加載後清除了其內容。

我已經激活了Hibernate日誌,當我的代碼訪問孩子們收集我看到:

 

    [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (http-localhost/127.0.0.1:8080-1) Obtaining JDBC connection 
    [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (http-localhost/127.0.0.1:8080-1) Obtained JDBC connection 
    [org.hibernate.loader.Loader] (http-localhost/127.0.0.1:8080-1) Result set contains (possibly empty) collection: [com.dat.abs.run.PadreBean.iFiglioBeans#component[iPadreK1,iPadreK2]{iPadreK2=p1, iPadreK1=p1}] 
    [org.hibernate.loader.Loader] (http-localhost/127.0.0.1:8080-1) Result set row: 0 
    [org.hibernate.loader.Loader] (http-localhost/127.0.0.1:8080-1) Result row: EntityKey[com.dat.abs.run.FiglioBean#component[iFiglioK1,iFiglioK2,iPadrePadreK1,iPadrePadreK2]{iFiglioK2=1     , iPadrePadreK1=p1     , iFiglioK1=1     , iPadrePadreK2=p1     }] 
    [org.hibernate.loader.Loader] (http-localhost/127.0.0.1:8080-1) Found row of collection: [com.dat.abs.run.PadreBean.iFiglioBeans#component[iPadreK1,iPadreK2]{iPadreK2=p1     , iPadreK1=p1     }] 
    [org.hibernate.loader.Loader] (http-localhost/127.0.0.1:8080-1) Result set row: 1 
    [org.hibernate.loader.Loader] (http-localhost/127.0.0.1:8080-1) Result row: EntityKey[com.dat.abs.run.FiglioBean#component[iFiglioK1,iFiglioK2,iPadrePadreK1,iPadrePadreK2]{iFiglioK2=f1     , iPadrePadreK1=p1     , iFiglioK1=f1     , iPadrePadreK2=p1     }] 
    [org.hibernate.loader.Loader] (http-localhost/127.0.0.1:8080-1) Found row of collection: [com.dat.abs.run.PadreBean.iFiglioBeans#component[iPadreK1,iPadreK2]{iPadreK2=p1     , iPadreK1=p1     }] 
    [org.hibernate.engine.internal.TwoPhaseLoad] (http-localhost/127.0.0.1:8080-1) Resolving associations for [com.dat.abs.run.FiglioBean#component[iFiglioK1,iFiglioK2,iPadrePadreK1,iPadrePadreK2]{iFiglioK2=1     , iPadrePadreK1=p1     , iFiglioK1=1     , iPadrePadreK2=p1     }] 
    [org.hibernate.engine.internal.TwoPhaseLoad] (http-localhost/127.0.0.1:8080-1) Done materializing entity [com.dat.abs.run.FiglioBean#component[iFiglioK1,iFiglioK2,iPadrePadreK1,iPadrePadreK2]{iFiglioK2=1     , iPadrePadreK1=p1     , iFiglioK1=1     , iPadrePadreK2=p1     }] 
    [org.hibernate.engine.internal.TwoPhaseLoad] (http-localhost/127.0.0.1:8080-1) Resolving associations for [com.dat.abs.run.FiglioBean#component[iFiglioK1,iFiglioK2,iPadrePadreK1,iPadrePadreK2]{iFiglioK2=f1     , iPadrePadreK1=p1     , iFiglioK1=f1     , iPadrePadreK2=p1     }] 
    [org.hibernate.engine.internal.TwoPhaseLoad] (http-localhost/127.0.0.1:8080-1) Done materializing entity [com.dat.abs.run.FiglioBean#component[iFiglioK1,iFiglioK2,iPadrePadreK1,iPadrePadreK2]{iFiglioK2=f1     , iPadrePadreK1=p1     , iFiglioK1=f1     , iPadrePadreK2=p1     }] 
    [org.hibernate.engine.loading.internal.CollectionLoadContext] (http-localhost/127.0.0.1:8080-1) 2 collections were found in result set for role: com.dat.abs.run.PadreBean.iFiglioBeans 
    [org.hibernate.engine.loading.internal.CollectionLoadContext] (http-localhost/127.0.0.1:8080-1) Collection fully initialized: [com.dat.abs.run.PadreBean.iFiglioBeans#component[iPadreK1,iPadreK2]{iPadreK2=p1, iPadreK1=p1}] 
    [org.hibernate.engine.loading.internal.CollectionLoadContext] (http-localhost/127.0.0.1:8080-1) Collection fully initialized: [com.dat.abs.run.PadreBean.iFiglioBeans#component[iPadreK1,iPadreK2]{iPadreK2=p1     , iPadreK1=p1     }] 
    [org.hibernate.engine.loading.internal.CollectionLoadContext] (http-localhost/127.0.0.1:8080-1) 2 collections initialized for role: com.dat.abs.run.PadreBean.iFiglioBeans 
    [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (http-localhost/127.0.0.1:8080-1) Releasing JDBC connection 
    [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (http-localhost/127.0.0.1:8080-1) Released JDBC connection 
    [org.hibernate.loader.Loader] (http-localhost/127.0.0.1:8080-1) Done loading collection 
    [org.hibernate.event.internal.AbstractFlushingEventListener] (http-localhost/127.0.0.1:8080-1) Processing flush-time cascades 
    
    [org.hibernate.event.internal.AbstractFlushingEventListener] (http-localhost/127.0.0.1:8080-1) Dirty checking collections 
    [org.hibernate.engine.internal.Collections] (http-localhost/127.0.0.1:8080-1) Collection found: [com.dat.abs.run.PadreBean.iFiglioBeans#component[iPadreK1,iPadreK2]{iPadreK2=p1, iPadreK1=p1}], was: [com.dat.abs.run.PadreBean.iFiglioBeans#component[iPadreK1,iPadreK2]{iPadreK2=p1, iPadreK1=p1}] (initialized) 
    [org.hibernate.engine.internal.Collections] (http-localhost/127.0.0.1:8080-1) Found collection with unloaded owner: [com.dat.abs.run.PadreBean.iFiglioBeans#component[iPadreK1,iPadreK2]{iPadreK2=p1     , iPadreK1=p1     }] 
    [org.hibernate.event.internal.AbstractFlushingEventListener] (http-localhost/127.0.0.1:8080-1) Flushed: 0 insertions, 0 updates, 0 deletions to 4 objects 
    [org.hibernate.event.internal.AbstractFlushingEventListener] (http-localhost/127.0.0.1:8080-1) Flushed: 0 (re)creations, 0 updates, 0 removals to 2 collections 
    [org.hibernate.internal.util.EntityPrinter] (http-localhost/127.0.0.1:8080-1) Listing entities: 
    [org.hibernate.internal.util.EntityPrinter] (http-localhost/127.0.0.1:8080-1) com.dat.abs.run.FiglioBean{iDescrizioneFiglio=     , iFiglioPK=component[iFiglioK1,iFiglioK2,iPadrePadreK1,iPadrePadreK2]{iFiglioK2=1     , iPadrePadreK1=p1     , iFiglioK1=1     , iPadrePadreK2=p1     }, iABSUniqueId=249} 
    [org.hibernate.internal.util.EntityPrinter] (http-localhost/127.0.0.1:8080-1) com.dat.abs.run.ABSOutQueueBean{iCreationDateTime=2012-02-17 07:51:10.0, iLastUpdateUser=null, iCreationUser=system     , iLastUpdateDateTime=null, iPrintServiceName=null, iId=component[iName]{iName=#01     }, iDescription=01            } 
    [org.hibernate.internal.util.EntityPrinter] (http-localhost/127.0.0.1:8080-1) com.dat.abs.run.FiglioBean{iDescrizioneFiglio=f1     , iFiglioPK=component[iFiglioK1,iFiglioK2,iPadrePadreK1,iPadrePadreK2]{iFiglioK2=f1     , iPadrePadreK1=p1     , iFiglioK1=f1     , iPadrePadreK2=p1     }, iABSUniqueId=248} 
    [org.hibernate.internal.util.EntityPrinter] (http-localhost/127.0.0.1:8080-1) com.dat.abs.run.PadreBean{iPadrePK=component[iPadreK1,iPadreK2]{iPadreK2=p1, iPadreK1=p1}, iAbsOutQueueBean=com.dat.abs.run.ABSOutQueueBean#component[iName]{iName=#01     }, iDescription=p1     , iABSUniqueId=247, iFiglioBeans=[]} 
    [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (http-localhost/127.0.0.1:8080-1) Releasing JDBC connection 
    [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (http-localhost/127.0.0.1:8080-1) Aggressively releasing JDBC connection 
 

正如你看到Hibernate的首次加載了孩子,但清洗後。 見與前行連接數:發現收集與卸載業主:消息

的問題是,DBMS用填充字符列中的空白,已經我已經說過,如果我插入的所有字符在我的按鍵列中也是如此。

那麼,有沒有解決方案讓我的孩子收集由Hibernate使用CHAR列作爲主鍵不完全填充?

在此先感謝您的時間

回答

1

我已經解決了編寫我自己的用戶類型的問題。 我張貼在這裏,因爲可能對其他有用。 測試都使用DB2 9.x和甲骨文12C

 

    package com.dat.abs.run; 

    import java.io.Serializable; 
    import java.sql.PreparedStatement; 
    import java.sql.ResultSet; 
    import java.sql.SQLException; 
    import java.sql.Types; 

    import org.hibernate.HibernateException; 
    import org.hibernate.engine.spi.SessionImplementor; 
    import org.hibernate.usertype.UserType; 

    public class ABSChar implements UserType 
    { 
     public int[] sqlTypes() 
     { 
      return new int[] 
      { 
       Types.CHAR, 
      }; 
     } 
     public Class returnedClass() 
     { 
      return String.class; 
     } 
     public boolean equals(Object x, Object y) throws HibernateException 
     { 
      return (x == y) || (x != null && y != null && (x.equals(y))); 
     } 
     public Object deepCopy(Object value) throws HibernateException 
     { 
      if (value == null) 
      return null; 
      return new String((String) value); 
     } 
     public boolean isMutable() 
     { 
      return false; 
     } 
     @Override 
     public int hashCode(Object aValue) throws HibernateException 
     { 
      return aValue.hashCode(); 
     } 
     @Override 
     public Object replace(Object aOriginal, Object aTarget, Object aOwner) throws HibernateException 
     { 
      return aOriginal; 
     } 
     @Override 
     public Object assemble(Serializable aChaced, Object aOwner) throws HibernateException 
     { 
      return aChaced; 
     } 
     @Override 
     public Serializable disassemble(Object aValue) throws HibernateException 
     { 
      return (Serializable) aValue; 
     } 
     @Override 
     public Object nullSafeGet(ResultSet aRs, String[] aNames, SessionImplementor aSession, Object aOwner) throws HibernateException, SQLException 
     { 
      String val = aRs.getString(aNames[0]); 
      if (val == null) 
      { 
      return null; 
      } 
      return val.trim(); 
     } 
     @Override 
     public void nullSafeSet(PreparedStatement aStatament, Object aValue, int aIndex, SessionImplementor aSession) throws HibernateException, SQLException 
     { 
      aStatament.setString(aIndex, (String) aValue); 
     } 
    } 

而這款以使用bean類的類型

 

     @Type(type = "com.dat.abs.run.ABSChar") 
     @Column(name = "PADREPADREK1", length = 20) 
     private String iPadrePadreK1; 
     @Type(type = "com.dat.abs.run.ABSChar") 
     @Column(name = "PADREPADREK2", length = 20) 
     private String iPadrePadreK2; 
     @Type(type = "com.dat.abs.run.ABSChar") 
     @Column(name = "FIGLIOK1", length = 20) 
     private String iFiglioK1; 
     @Type(type = "com.dat.abs.run.ABSChar") 
     @Column(name = "FIGLIOK2", length = 20)