Hibernate 3,addScalar(),UserType [Types.BIGINT,Types.VARCHAR] 2 colums

时间:2017-10-02 09:32:38

标签: oracle hibernate

我有一个CreditCard域名:

class CreditCard {

Long Id 
Price value 
...

static mapping = {
        value type: PriceType,{
            column name: "VALUE_AMOUNT"
            column name: "VALUE_CURRENCY"
        } 
}

我想创建一个报告,因此我创建了自定义类型:

Type priceTypeLocator = new TypeLocatorImpl(new TypeResolver()).custom(PriceType)

和查询:

def result = getSession().createSQLQuery("select ID, VALUE_AMOUNT, VALUE_CURRENCY from CreditCard")
.addScalar("ID", StandardBasicTypes.LONG)
.addScalar("VALUE_AMOUNT",priceTypeLocator) //this wont work
.addScalar("VALUE_CURRENCY",priceTypeLocator) //this wont work
.setResultTransformer(Transformers.aliasToBean(CreditCard.class))
.list();

如何将这2个值(VALUE_AMOUNT和VALUE_CURRENCY)与CreditCard域中的值字段绑定在一起?

2 个答案:

答案 0 :(得分:1)

我终于解决了使用标准类型和设置器的问题:

 .addScalar('requestedAmountAmo', StandardBasicTypes.BIG_DECIMAL)
 .addScalar('requestedAmountCur', StandardBasicTypes.STRING)

然后我设定了价格:

 void setRequestedAmountAmo(BigDecimal requestedAmountAmo) {
    requestedAmount = new Price(amount:requestedAmountAmo, currency: '')
}

void setRequestedAmountCur(String requestedAmountCur) {
    requestedAmount.currency = requestedAmountCur
}

答案 1 :(得分:0)

您可以使用此方法:

public static Type getPropertyType(Session session, Class entityClass, String propertyName) {
    return session.getSessionFactory().getClassMetadata(entityClass).getPropertyType(propertyName);
}

并以这种方式使用它:

.addScalar("VALUE_AMOUNT", getPropertyType(session, CreditCard.class, "value_amount_poperty_name"))