1

我得到了下一个例外:

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);
        }

在日志中,我看到下一条记录显示昆德拉向 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
}

谁能解释一下为什么昆德拉将字符串“1”转换为整数1?我怎样才能避免这种情况?

谢谢

4

2 回答 2

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并将您的in替换TypedQuerywhere apc.myid.promoCodeId.

您可以参考这里以获取有关复合键的更多信息。

于 2015-04-06T13:53:50.220 回答
0

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

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

于 2015-04-07T14:21:20.030 回答