Kundera cast String" 1"查询中的整数1

时间:2015-04-04 12:49:14

标签: java cassandra jpql cql kundera

我有下一个例外:

Caused by: com.impetus.kundera.KunderaException: com.datastax.driver.core.exceptions.InvalidQueryException: Invalid INTEGER constant (1) for promo_code_id of type ascii
    at com.impetus.kundera.client.cassandra.dsdriver.DSClient.execute(DSClient.java:510) [kundera-cassandra-ds-driver.jar:]
    at com.impetus.kundera.client.cassandra.dsdriver.DSClient.executeQuery(DSClient.java:415) [kundera-cassandra-ds-driver.jar:]
    at com.impetus.client.cassandra.query.CassQuery.recursivelyPopulateEntities(CassQuery.java:245) [kundera-cassandra.jar:]
    at com.impetus.kundera.query.QueryImpl.fetch(QueryImpl.java:1266) [kundera-core.jar:]
    at com.impetus.kundera.query.QueryImpl.getResultList(QueryImpl.java:187) [kundera-core.jar:]
    at com.impetus.kundera.query.KunderaTypedQuery.getResultList(KunderaTypedQuery.java:250) [kundera-core.jar:]

执行下一段代码时:

public List<ActivatedPromoCode> findRegistrationPromoBetween(List<String> promoCodeIds, Date startDate, Date endDate)
            throws DAOException {
        try {
            if (promoCodeIds == null ) return null;

            TypedQuery<ActivatedPromoCode> query = getEM().createQuery(
                    "select apc from ActivatedPromoCode apc where apc.promoCodeId in ?1 " +
                            "and apc.isRegistrationPromo = true " +
                            "and apc.activationTimestamp > ?2 and apc.activationTimestamp < ?3",
                    ActivatedPromoCode.class);
            query.setParameter(1, promoCodeIds);
            query.setParameter(2, startDate);
            query.setParameter(3, endDate);

            try {
                return query.getResultList();
            } catch (NoResultException e) {
                return null;
            }
        } catch (Exception e) {
            throw new DAOException("Search activated promo codes activated during registration", e);
        }

在日志中,我看到下一条记录,显示Kundera向Cassandra发送的查询:

09:35:22,963 ERROR [com.impetus.kundera.client.cassandra.dsdriver.DSClient] (Thread-0 (HornetQ-client-global-threads-1819772796)) Error while executing query SELECT * FROM "activated_promo_code" WHERE "promo_code_id" IN (1) AND "is_registration_promo" = true AND "activation_timestamp" > '1427857200000' AND "activation_timestamp" < '1428116400000' LIMIT 100  ALLOW FILTERING.

数据库中结构的定义:

 describe table activated_promo_code;

CREATE TABLE ustaxi.activated_promo_code (
    id ascii,
    promo_code_id ascii,
    activation_timestamp timestamp,
    code ascii,
    current_target_bonus int,
    is_active boolean,
    is_registration_promo boolean,
    target_id ascii,
    used_counts int,
    PRIMARY KEY (id, promo_code_id)
) WITH CLUSTERING ORDER BY (promo_code_id ASC)
    AND bloom_filter_fp_chance = 0.01
    AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
    AND comment = ''
    AND compaction = {'min_threshold': '4', 'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32'}
    AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99.0PERCENTILE';
CREATE INDEX activatedpromocodebypromocodeid ON ustaxi.activated_promo_code (promo_code_id);
CREATE INDEX activatedpromocodebyactivationts ON ustaxi.activated_promo_code (activation_timestamp);
CREATE INDEX activatedpromocodebycode ON ustaxi.activated_promo_code (code);
CREATE INDEX activatedpromocodebyregistrationflag ON ustaxi.activated_promo_code (is_registration_promo);
CREATE INDEX activatedpromocodebytargetid ON ustaxi.activated_promo_code (target_id);

班级声明:

@Entity
@Table(name = "activated_promo_code")
public class ActivatedPromoCode {

    @Id
    @GeneratedValue
    @Column(name = "id", nullable = false, unique = true)
    private String id;

    @Column(name = "promo_code_id")
    private String promoCodeId;

    @Column(name = "code")
    private String code;

    @Column(name = "target_id")
    private String targetId;

    @Column(name = "activation_timestamp")
    private Date activationTimestamp;

    @Column(name = "current_target_bonus")
    private Integer currentTargetBonus;

    @Column(name = "is_active")
    private Boolean isActive;

    @Column(name = "used_counts")
    private Integer usedCounts;

    @Column(name = "is_registration_promo")
    private Boolean isRegistrationPromo;

    // Getters and setters
}

任何人都可以解释为什么昆德拉会改变字符串&#34; 1&#34;整数1?我怎么能避免这种情况呢?

由于

2 个答案:

答案 0 :(得分:2)

Kundera不会将String转换为Integer。表的映射 - 实体不正确(表中有复合键,但未在实体中定义)。

为了在使用Kundera的Cassandra中使用复合键,您需要将其定义为embeddedId

可嵌入课程: -

@Embeddable
public class Myid
{
    @Column(name = "id", nullable = false, unique = true)
    private String id;

    @Column(name = "promo_code_id")
    private String promoCodeId;

    //setters and getters
}

实体类: -

@Entity
@Table(name = "activated_promo_code")
public class ActivatedPromoCode {

@EmbeddedId
private Myid myid;           //EmbeddedId

@Column(name = "code")
private String code;

@Column(name = "target_id")
private String targetId;

@Column(name = "activation_timestamp")
private Date activationTimestamp;

@Column(name = "current_target_bonus")
private Integer currentTargetBonus;

@Column(name = "is_active")
private Boolean isActive;

@Column(name = "used_counts")
private Integer usedCounts;

@Column(name = "is_registration_promo")
private Boolean isRegistrationPromo;

// Getters and setters
}

where apc.promoCodeId中的TypedQuery替换为where apc.myid.promoCodeId

有关复合键的更多信息,请参阅this

答案 1 :(得分:0)

所以,问题不在于昆德拉,而是在模型类定义中。类包含映射到一个数据库字段的2个字段。有时字段promo_code_id会转换为String并且我们得到了正确的工作,有时它会转换为PromoCode并且我们得到了错误的工作。 因此,解决方案是使用更合适的模式定义。

与讨论聊天: http://chat.stackoverflow.com/rooms/74612/discussion-between-karthik-manchala-and-dolzhenok