valchkou / cassandra-driver-mapping

JPA addon for DataStax Java Driver for Cassandra
58 stars 24 forks source link

any-to-any mapping #37

Closed DBatOWL closed 9 years ago

DBatOWL commented 9 years ago

If I start with a class (ABC) containing the fields A, B, C and then read an entity (BCD) from the DB returning fields B, C, D, when I use this feature to assign the results to the ABC class, I get an IllegalArgumentException stating the A is not a column in the metadata. The description of the feature says that extra fields in BCD not in ABC will be ignored, is there are reason why are those of the receiving class should throw an exception instead of also being ignored? Is there some configuration I can use to alter this behavior? I know I can just turn off INFO logging and the message goes away but then I might miss other potentially important information.

valchkou commented 9 years ago

Could you please provide me your abc and bcd samples.

I have unittest which works fine: https://github.com/valchkou/cassandra-driver-mapping/blob/master/src/test/java/com/datastax/driver/mapping/MappingSessionTest.java

DBatOWL commented 9 years ago
package test;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.mapping.MappingSession;

public class Main {

    static final String node = "127.0.0.1";

    static Cluster cluster;
    static Session session;
    static String keyspace = "testing";

    static MappingSession mapper;

    public static void main(String[] args) {

        cluster = Cluster.builder().addContactPoint(node).build();
        session = cluster.connect();
        session.execute("CREATE KEYSPACE IF NOT EXISTS " + keyspace //
                + " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }");
        session.execute("USE " + keyspace);
        mapper = new MappingSession(keyspace, session);

        BCD bcdCreate = new BCD();
        bcdCreate.setB("b");
        bcdCreate.setC("c");
        bcdCreate.setD("d");
        mapper.save(bcdCreate);

        ResultSet rs = session.execute("SELECT b, c, d FROM bcd");

        // This will log "IllegalArgumentException: A is not a column defined in this metadata"
        List<ABC> result = mapper.getFromResultSet(ABC.class, rs);

        session.close();
        cluster.close();
    }

    public static class ABC {
        private String A;
        private String B;
        private String C;
        public String getA() { return A; }
        public void setA(String a) { A = a; }
        public String getB() { return B; }
        public void setB(String b) { B = b; }
        public String getC() { return C; }
        public void setC(String c) { C = c; }
    }

    @Table(name="bcd")
    public static class BCD {
        @Id
        private String B;
        @Column
        private String C;
        @Column
        private String D;
        public String getB() { return B; }
        public void setB(String b) { B = b; }
        public String getC() { return C; }
        public void setC(String c) { C = c; }
        public String getD() { return D; }
        public void setD(String d) { D = d; }
    }
}
valchkou commented 9 years ago

got it! I though it blows with exception but it just logs it. I can see that log and agree it shouldn't be there

valchkou commented 9 years ago

fixed and released. note the latest ver is 2.1.1