0

我正在尝试使用以下 Criteria 查询在 Hibernate 中提取记录。

Security result = (Security) getSession().createCriteria(Security.class)
        .add(  Restrictions.eq("symbol", symbol) ).uniqueResult();

symbol 属性是唯一的varchar 类型(股票代码),symbol 参数是String。

通常这工作得很好,但只要符号在名称中包含连字符,例如“CU”,我就会得到一个 AssertionFailure 异常。

知道我做错了什么或如何解决吗?


一些背景......

这发生在一个长期交易中,我在其中存储来自纽约证券交易所和纳斯达克的大量股票(证券)的日内统计数据(从雅虎提取的股票的当前价格)。

到此为止,已有数百只证券通过了循环。它们已被“保存”,但交易尚未提交。我在缓冲区(?)满之前把它关掉了。只有当涉及符号中带有连字符的证券时,它才会抛出此异常。

这是调用参数....

security = securityDAO.findBySymbol(record[0]);

SecurityDAO 中的完整方法......

public Security findBySymbol(String symbol){
    log.debug("finding Security by symbol");
    try{

        Security result = 
            (Security) getSession().createCriteria(Security.class)
        .add(  Restrictions.eq("symbol", symbol)).uniqueResult();

        if (result == null)
            return null;

        return result;
    } catch (RuntimeException re) {
        log.error("Failed to find security by symbol.", re);
        throw re;
    }
}

抛出的异常...

org.hibernate.AssertionFailure: null id in com.securityscanner.hibernate.IntradayStat entry (don't flush the Session after an exception occurs)
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:78)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:187)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:58)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:997)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1590)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:328)
at com.securityscanner.hibernate.SecurityDAO.findBySymbol(SecurityDAO.java:187)
at com.securityscanner.ScanStatsTask.storeCurrentStats(ScanStatsTask.java:196)
at com.securityscanner.ScanStatsTask.run(ScanStatsTask.java:99)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)

安全性扩展 AbstractSecurity.................................................

/

**
 * AbstractSecurity entity provides the base persistence definition of the
 * Security entity. @author MyEclipse Persistence Tools
 */

public abstract class AbstractSecurity implements java.io.Serializable {

    // Fields

    private Integer securityId;
    private Exchange exchange;
    private String name;
    private String symbol;
    private String securityType;
    private String description;
    private Boolean skip;
    private Set dailyStats = new HashSet(0);
    private Set intradayStats = new HashSet(0);

    // Constructors

    /** default constructor */
    public AbstractSecurity() {
    }

    /** full constructor */
    public AbstractSecurity(Exchange exchange, String name, String symbol,
            String securityType, String description, Boolean skip,
            Set dailyStats, Set intradayStats) {
        this.exchange = exchange;
        this.name = name;
        this.symbol = symbol;
        this.securityType = securityType;
        this.description = description;
        this.skip = skip;
        this.dailyStats = dailyStats;
        this.intradayStats = intradayStats;
    }

    // Property accessors

    public Integer getSecurityId() {
        return this.securityId;
    }

    public void setSecurityId(Integer securityId) {
        this.securityId = securityId;
    }

    public Exchange getExchange() {
        return this.exchange;
    }

    public void setExchange(Exchange exchange) {
        this.exchange = exchange;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSymbol() {
        return this.symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public String getSecurityType() {
        return this.securityType;
    }

    public void setSecurityType(String securityType) {
        this.securityType = securityType;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Boolean getSkip() {
        return this.skip;
    }

    public void setSkip(Boolean skip) {
        this.skip = skip;
    }

    public Set getDailyStats() {
        return this.dailyStats;
    }

    public void setDailyStats(Set dailyStats) {
        this.dailyStats = dailyStats;
    }

    public Set getIntradayStats() {
        return this.intradayStats;
    }

    public void setIntradayStats(Set intradayStats) {
        this.intradayStats = intradayStats;
    }

}
4

1 回答 1

1

帕特里克,错误不在选择中,而是在之前的某个地方。

Hibernate 保留所有更新或创建的对象的列表,当您刷新会话或执行任何其他强制刷新的操作(例如选择)时,它会将所有脏对象保存到数据库中。

From the stack trace, it looks like you have saved / updated a new instance of IntradayStat without an id and hibernate is expecting one.

于 2012-09-15T07:12:57.300 回答