对带有连字符的字符串的hibernate限制会抛出assertionfailure异常

时间:2012-09-14 18:33:16

标签: hibernate restrictions

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

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

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

通常情况下这很好用,但只要符号在名称中包含连字符,例如“C-U”,我就会得到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;
    }

}

1 个答案:

答案 0 :(得分:1)

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

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

从堆栈跟踪中,看起来你已经保存/更新了一个没有id的IntradayStat的新实例,而hibernate正在期待它。