我是JAVA中Hibernate的初学者,不知道如何解决这个问题
我有两张桌子:Usuario和Certificado,我想创建一个关系和他们的关系。
Class Certificado:
@Entity
@Table(name = "Certificado",
uniqueConstraints = { @UniqueConstraint( columnNames = { "identifierUnique", "handleUser" } ) } )
public class Certificado {
@Id
@GenericGenerator(name="generator", strategy="increment")
@GeneratedValue(generator="generator")
private int handle;
@Column(length = 255)
@Column
private int handleUser;
@ManyToOne
@JoinColumn(name="handle_user", referencedColumnName="handle")
private Usuario usuario;
}
Class Usuario:
@Entity
@Table(name = "Usuario")
public class Usuario {
@Id
@GenericGenerator(name="generator", strategy="increment")
@GeneratedValue(generator="generator")
private int handle;
@Column(length = 255, unique = true)
private String email;
@Column(length = 20)
private String password;
@OneToMany(mappedBy="usuario", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Certificado> certificados;
...
}
当我执行SQL函数时:
public static List<Object[]> loadCertificado30DaysBeforeExperation() throws Exception{
Session.begin();
try {
try {
String select = "SELECT c FROM Certificado c "
+ "LEFT JOIN c.handleUser AS user";
return Session.list(select);
} catch (Exception e) {
throw e;
}
} finally {
Session.commit();
}
}
错误:
java.lang.NullPointerException
堆栈跟踪:
java.lang.NullPointerException
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:393)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3645)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3431)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3309)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:249)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:184)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:137)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
at com.sun.proxy.$Proxy15.createQuery(Unknown Source)
at br.com.persistence.Session.list(Session.java:96)
at br.com.utils.ServletUtils.loadCertificado30DaysBeforeExperation(ServletUtils.java:134)
at br.com.monitoramento.MonitorThread.run(MonitorThread.java:25)
答案 0 :(得分:2)
Hibernate的HQL不支持间隔(and generally has extremely poor date maths support)。
您需要使用本机SQL,或通过在Java端本地进行日期计算来表达不带interval
的查询。
或者您可以注册用户定义的SQL函数并创建一个PostgreSQL函数来执行您想要的日期数学运算。
请参阅:
还有更多。
答案 1 :(得分:0)
您是否尝试过删除''从'30' - &gt;间隔30天。 或逃避'30' - &gt; \ '30 \”。