Cassandra映射器未映射到类

时间:2016-06-02 19:46:33

标签: java cassandra

我正在构建REST API,我有访问器

    @Accessor
public interface ActiveBidAccessor {
    @Query("SELECT * FROM keyspace.active_bid WHERE username = :username")
    public Result<ActiveBid> getAllByUsersname(@Param("username") String username);


    @Query("SELECT * FROM keyspace.active_bid")
    public Result<ActiveBid> getAll();
}

和ActiveBid类

@Table(keyspace = "keyspace", name = "active_bid", readConsistency = "QUORUM", writeConsistency = "QUORUM", caseSensitiveKeyspace = false, caseSensitiveTable = false)
public class ActiveBid {
    @PartitionKey
    UUID uid;
    @ClusteringColumn
    @Column(name = "username")
    String username;
    @Column(name = "project_name")
    String projectName;
    @Column(name = "project_link")
    String projectLink;
    @Column(name = "total_bid")
    int totalBid;
    @Column(name = "user_bid")
    long userBid;
    @Column(name = "avg_bid")
    long avgBid;
    @Column(name = "end_date")
    Date endDate;

    public ActiveBid(UUID uid, String username, String projectName, String projectLink, int totalBid, long userBid,
            long avgBid, Date endDate) {
        super();
        this.uid = uid;
        this.username = username;
        this.projectName = projectName;
        this.projectLink = projectLink;
        this.totalBid = totalBid;
        this.userBid = userBid;
        this.avgBid = avgBid;
        this.endDate = endDate;
    }

    public UUID getUid() {
        return uid;
    }

    public void setUid(UUID uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getProjectLink() {
        return projectLink;
    }

    public void setProjectLink(String projectLink) {
        this.projectLink = projectLink;
    }

    public int getTotalBid() {
        return totalBid;
    }

    public void setTotalBid(int totalBid) {
        this.totalBid = totalBid;
    }

    public long getUserBid() {
        return userBid;
    }

    public void setUserBid(long userBid) {
        this.userBid = userBid;
    }

    public long getAvgBid() {
        return avgBid;
    }

    public void setAvgBid(long avgBid) {
        this.avgBid = avgBid;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(uid, username, projectName, projectLink, totalBid, userBid, avgBid, endDate);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof ActiveBid) {
            ActiveBid that = (ActiveBid) obj;
            return Objects.equal(this.uid, that.uid) && Objects.equal(this.username, that.username)
                    && Objects.equal(this.projectName, that.projectName)
                    && Objects.equal(this.projectLink, that.projectLink) && Objects.equal(this.totalBid, that.totalBid)
                    && Objects.equal(this.userBid, that.userBid) && Objects.equal(this.avgBid, that.avgBid)
                    && Objects.equal(this.endDate, that.endDate);
        }
        return false;
    }

和我的球衣请求映射器,其中包含获取数据的逻辑

@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public List<ActiveBid> getTrackInJSON() {
    logger.info("LoginApi: Returning the active bid");
    List<ActiveBid> activeBidList = new ArrayList<ActiveBid>();

    MappingManager manager = new MappingManager(cassDB.getSession());
    ActiveBidAccessor activeBidAccessor = manager.createAccessor(ActiveBidAccessor.class);
    Result<ActiveBid> activeBidResult = activeBidAccessor.getAllByUsersname("zakir");
    for(ActiveBid bid:activeBidResult){
        activeBidList.add(bid);
    }

    return activeBidList;
}

我收到以下错误

  

引起:java.lang.RuntimeException:无法创建实例   com.xyz.v1.ActiveBid at   com.datastax.driver.mapping.ReflectionMapper.newEntity(ReflectionMapper.java:47)     在com.datastax.driver.mapping.Result.map(Result.java:40)at   com.datastax.driver.mapping.Result.one(Result.java:87)at   com.datastax.driver.mapping.Mapper $ 1.apply(Mapper.java:82)at   com.datastax.driver.mapping.Mapper $ 1.apply(Mapper.java:79)at   com.google.common.util.concurrent.Futures $ 1.适用(Futures.java:713)     在   com.google.common.util.concurrent.Futures $ ChainingListenableFuture.run(Futures.java:861)     在   com.google.common.util.concurrent.MoreExecutors $ SameThreadExecutorService.execute(MoreExecutors.java:297)     在   com.google.common.util.concurrent.ExecutionList.executeListener(ExecutionList.java:156)     在   com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:145)     在   com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:185)     在   com.google.common.util.concurrent.Futures $ ChainingListenableFuture $ 1.run(Futures.java:872)     在   com.google.common.util.concurrent.MoreExecutors $ SameThreadExecutorService.execute(MoreExecutors.java:297)     在   com.google.common.util.concurrent.ExecutionList.executeListener(ExecutionList.java:156)     在   com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:145)     在   com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:185)     在   com.datastax.driver.core.DefaultResultSetFuture.onSet(DefaultResultSetFuture.java:174)     在   com.datastax.driver.core.RequestHandler.setFinalResult(RequestHandler.java:177)     在   com.datastax.driver.core.RequestHandler.access $ 2500(RequestHandler.java:43)     在   com.datastax.driver.core.RequestHandler $ SpeculativeExecution.setFinalResult(RequestHandler.java:792)     在   com.datastax.driver.core.RequestHandler $ SpeculativeExecution.onSet(RequestHandler.java:467)     在   com.datastax.driver.core.Connection $ Dispatcher.channelRead0(Connection.java:1013)     在   com.datastax.driver.core.Connection $ Dispatcher.channelRead0(Connection.java:936)     在   io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318)     在   io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304)     在   io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:266)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318)     在   io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304)     在   io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318)     在   io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304)     在   io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:276)     在   io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:263)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318)     在   io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304)     在   io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846)     在   io.netty.channel.nio.AbstractNioByteChannel $ NioByteUnsafe.read(AbstractNioByteChannel.java:131)     在   io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)     在   io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)     在   io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)     在io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)at   io.netty.util.concurrent.SingleThreadEventExecutor $ 2.run(SingleThreadEventExecutor.java:112)     ......还有1个

有人可以帮我解决这个问题吗,我不知道Cassandra会不会这么做。

2 个答案:

答案 0 :(得分:6)

驱动程序不善于报告异常原因。 stacktrace points to this

@Override
public T newEntity() {
    try {
        return entityClass.newInstance();
    } catch (Exception e) {
        throw new RuntimeException("Can't create an instance of " + entityClass.getName());
    }
}

不幸吞下了这个例外。

同样,读取该代码时,似乎发生异常,因为您的实体类中没有可访问的无参数构造函数。

只需添加一个

public ActiveBid() {}

文档here并未明确说明,但所有示例都使用此类构造函数。

答案 1 :(得分:2)

经典问题,您的班级ActiveBid应尊重Java Beans惯例,例如提供默认(public&amp; no arg)构造函数