目前分類:HIBERNATE (5)

瀏覽方式: 標題列表 簡短摘要

錯誤訊息如下,需要先確認DB Version欄位是否是null

我遇到的狀況是 A物件先做update (version is null), 接下來做select 的B物件( version is not null)
報錯報在B物件,

一直埋LOG才發現是前一條指令有問題

java.lang.NullPointerException
        at org.hibernate.type.LongType.next(LongType.java:79)
        at org.hibernate.type.LongType.next(LongType.java:40)
        at org.hibernate.engine.internal.Versioning.increment(Versioning.java:109)

文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

String sql = "...... from Tabal where col in (:colList)";

SQLQuery query = getCurrentSession().createSQLQuery(sql);
query.setParameterList("colList", String[] colValue);
query.list()

文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

再下SQL的時候使用query.setDate,發現傳入值Parameter有帶時、分、秒,但搜尋結果不如預期,查了一下發現忽略了一個很基本的東西

query.setDate 會自動過濾時分秒
條件有時分秒的時候,要使用query.setTimestamp

另外:java.sql.Date是有日期。java.util.Date包含時間部分的


文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

寫HQL的時候做運算發生傳回型態的錯誤

SELECT (round((PRD.Price-PRD.Cost)/PRD.Price, 3)*100) AS profit
FROM product PRD

HQL大概是上面這樣,然後回報下方問題。查了一下 應該是

「BigDecimal調用時,當傳入值的小數位數 大於設定的BigDecimal小數位數時,報出錯誤」

這樣的問題。

java.lang.ArithmeticException: Rounding necessary
        at java.math.BigDecimal.commonNeedIncrement(Unknown Source)
        at java.math.BigDecimal.needIncrement(Unknown Source)
        at java.math.BigDecimal.divideAndRound(Unknown Source)
        at java.math.BigDecimal.setScale(Unknown Source)
        at java.math.BigDecimal.toBigIntegerExact(Unknown Source)
        at org.hibernate.type.descriptor.java.BigIntegerTypeDescriptor.wrap(BigIntegerTypeDescriptor.java:105)
        at org.hibernate.type.descriptor.java.BigIntegerTypeDescriptor.wrap(BigIntegerTypeDescriptor.java:36)

如果是用SQLQuery的話

.addScalar("profit",  StandardBasicTypes.BIG_DECIMAL)

應該就可以了,

文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

使用hibernate 5 取序號異常狀況,DB為oracle,ID生成annotation設定如下


public class OrderDet implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO, generator="OrderDetSeq")
    @SequenceGenerator(name="OrderDetSeq", sequenceName="ORDERDET_SEQ")
    @Column(name="ID")
    private BigInteger id;

    public BigInteger getId() {
        return id;
    }

    public void setId(BigInteger id) {
        this.id = id;
    }
}

 

例如:DB SEQ為100,預期取出為101 (SEQ.nextval),但實際為60

這個問題在hibernate 4沒遇到,查了一下主要是以下原因

生成器演算法的調整(V4 to V5):
(old)   hi/lo: it uses the hi/lo algorithm and it’s equivalent to the original seqhilo generator.
(new) pooled: This optimizer uses a hi/lo optimization strategy, but the current in-memory identifiers highest boundary is extracted from an actual database sequence value.

hibernate 5會產生一個生成池供程式使用,避免多次去讀取DB,當池內數值用完,再去DB取一段SEQ回來(DB.SEQ會向後推一段空間),

文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()