0
votes

I am building a REST API and I have the accessor,

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

and ActiveBid class

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

and my jersey request mapper with the logic to get the data

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

and I am getting the following error

Caused by: java.lang.RuntimeException: Can't create an instance of com.xyz.v1.ActiveBid at com.datastax.driver.mapping.ReflectionMapper.newEntity(ReflectionMapper.java:47) at 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.apply(Futures.java:713) at com.google.common.util.concurrent.Futures$ChainingListenableFuture.run(Futures.java:861) at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:297) at com.google.common.util.concurrent.ExecutionList.executeListener(ExecutionList.java:156) at com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:145) at com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:185) at com.google.common.util.concurrent.Futures$ChainingListenableFuture$1.run(Futures.java:872) at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:297) at com.google.common.util.concurrent.ExecutionList.executeListener(ExecutionList.java:156) at com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:145) at com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:185) at com.datastax.driver.core.DefaultResultSetFuture.onSet(DefaultResultSetFuture.java:174) at com.datastax.driver.core.RequestHandler.setFinalResult(RequestHandler.java:177) at com.datastax.driver.core.RequestHandler.access$2500(RequestHandler.java:43) at com.datastax.driver.core.RequestHandler$SpeculativeExecution.setFinalResult(RequestHandler.java:792) at com.datastax.driver.core.RequestHandler$SpeculativeExecution.onSet(RequestHandler.java:467) at com.datastax.driver.core.Connection$Dispatcher.channelRead0(Connection.java:1013) at com.datastax.driver.core.Connection$Dispatcher.channelRead0(Connection.java:936) at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304) at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:266) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304) at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:276) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:263) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:304) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112) ... 1 more

Can someone please help me with this, I did not know Cassandra would be this difficult to work with.

2

2 Answers

6
votes

The driver is bad at reporting the cause of the exception. The 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());
    }
}

which unfortunately swallows the exception.

All the same, reading that code, the exception seems to occur because you don't have an accessible parameterless constructor in your entity class.

Just add one

public ActiveBid() {}

The documentation here doesn't make it explicit, but all their examples use such constructors.

3
votes

Classic problem, your class ActiveBid should respect the Java Beans convention e.g. provide a default (public & no arg) constructor