gpt4 book ai didi

mysql - Hibernate 按位运算函数未注册

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:41 24 4
gpt4 key购买 nike

我正在尝试在 hibernate 中注册一个函数以在 mysql 5 中执行按位与 (&) 和按位或 (|)。

到目前为止,我有一个自定义的 mysql 方言:

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">com.***.hibernate.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan">...</property>
</bean>

这是我的自定义方言代码:

public class MySQLDialect extends org.hibernate.dialect.MySQL5Dialect {
public MySQLDialect() {
super();
registerFunction("bitwise_and", new BitwiseAndFunction("bitwise_and"));
}
}

这是我的自定义函数:

public class BitwiseAndFunction extends StandardSQLFunction implements SQLFunction {

public BitwiseAndFunction(String name) {
super(name);
}

public BitwiseAndFunction(String name, Type type) {
super(name, type);
}

public String render(List args, SessionFactoryImplementor factory)
throws QueryException {
if (args.size() != 2) {
throw new IllegalArgumentException(
"the function must be passed 2 arguments");
}
StringBuffer buffer = new StringBuffer(args.get(0).toString());
buffer.append(" & ").append(args.get(1));
return buffer.toString();
}

问题是每当我在 HQL 查询中使用 bitwise_and 时,它都会给我一个长堆栈跟踪,开头为:

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: bitwise_and near line 1, column 354 [select sqrt(pow(rideRequest.startLatitude - p.latitude, 2) + pow(rideRequest.startLongitude - p.longitude, 2)) as distance, p.polyLine.rideOffer as rideOffer, p.polyLine.rideOffer.user, p.polyLine.rideOffer.user.role from com.freeride.lib.domain.Point p, com.freeride.lib.domain.RideRequest rideRequest where rideRequest.id=? and (rideOffer.permissions bitwise_and rideRequest.permissions) = rideRequest.permissions group by p.polyLine.rideOffer order by distance asc]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:79)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:276)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)

最佳答案

我找到了自己的解决方案。执行自定义 hql 函数的旧方法在 Hibernate 3.6.0 中已被弃用,因此在我的最新版本 (4.2.5) 中,我不得不使用:

import java.lang.Integer;
public class MyDialect extends org.hibernate.dialect.MySQL5Dialect {
public MyDialect() {
super();
//THIS DOES NOT WORK!
registerFunction("bitwise_andOne", new BitwiseAndFunction("poop"));
//THIS WORKS!
registerFunction("bitwise_andTwo", new SQLFunctionTemplate(IntegerType.INSTANCE, "(?1 & ?2)"));
}
}

基本上,第二种方法有效,而前一种方法无效。我不知道为什么。

您可以在任何 hql 查询中以编程方式调用 bitwise_and2。例如:

sessionFactory.getCurrentSession.createQuery("select * from people person where bitwise_andTwo(person.permissions, 7)

生成的 sql 类似于

从 people.permissions & 7 = permissions 的人中选择 *

这解决了我的问题。

关于mysql - Hibernate 按位运算函数未注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18834316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com