Java 昆德拉铸串“;1“;到查询中的整数1
我得到了下一个例外:Java 昆德拉铸串“;1“;到查询中的整数1,java,cassandra,jpql,cql,kundera,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(
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);
}
类别声明:
@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吗?我怎样才能避免呢
谢谢昆德拉不会将字符串转换为整数。表-实体的映射不正确(表中有复合键,但实体中未定义复合键) 为了使用昆德拉在Cassandra中使用复合键,您需要将其定义为
嵌入ID
可嵌入类:-
@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
}
并将TypedQuery
中的where apc.promoCodeId
替换为where apc.myid.promoCodeId
您可以参考有关复合键的更多信息。因此,问题不在昆德拉,而是在模型类定义中。类包含映射到一个数据库字段的2个字段。有时字段promo_code_id转换为字符串,我们得到了正确的工作,有时它转换为PromoCode,我们得到了错误的工作。 因此,解决方案是使用更合适的模式定义 与讨论人员聊天:
您的实体中
promoCodeId
字段的数据类型是什么?您能否共享您的实体(ActivatedPromoCode
)定义?DB中的数据类型:class中的ascii数据类型:Strings在复合键中没有交易,当我使用由两个字段组成的复合键和使用简单的一个字段键时,会出现问题。如果您告诉昆德拉没有转换类型,为什么查询看起来像SELECT*FROM“activated_promo_code”,其中(1)中的“promo_code_id”和“is_registration_promo”=true和“activation_timestamp”>'1427857200000'和“activation_timestamp”<'1428116400000'LIMIT 100允许在将字符串值插入IN运算符后进行筛选
?是的,您是对的,不应该发生这种情况。我也尝试过同样的方法,但效果很好。您使用的是哪个版本的昆德拉?最新版本为2.16,在对Cassandra的查询中,您在运算符中得到了字符串值“1”,如in('1')
?您的实体是否包含任何关系?是的。当前类有@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)@JoinColumn(name=“promo\u code\u id”,referencedColumnName=“id”)私有PromoCode PromoCode代码>
@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
}