Giter Site home page Giter Site logo

Comments (16)

doanduyhai avatar doanduyhai commented on August 15, 2024

You mean that given a @NotNull annotation, Achilles should set an empty collection to an entity when reading null columns from Cassandra isn't it ?

from achilles.

vladimirdolzhenko avatar vladimirdolzhenko commented on August 15, 2024

I suppose it is reasonable.

from achilles.

doanduyhai avatar doanduyhai commented on August 15, 2024

Be aware that setting @NotNull will also trigger validation on the collection when you persist the entity (provided you activate Bean validation).

I prefer to keep the @NotNull annotation for Bean Validation and create another one to inject empty collections/maps when deserializing data from Cassandra. Maybe @EmptyCollectionIfNull or something similar (any suggestion for a better name is welcomed).

It is better to separate both concerns and do not mix them

from achilles.

vladimirdolzhenko avatar vladimirdolzhenko commented on August 15, 2024

The simple case is : domain object has an empty set, the validation takes place on persist and it persists well - if I find a value, change any other value - I have to set empty collection instead of nulls - and all that null checks will be over the code.

from achilles.

doanduyhai avatar doanduyhai commented on August 15, 2024

"if I find a value, change any other value - I have to set empty collection instead of nulls - and all that null checks will be over the code."

In this case, I can make @EmptyCollectionIfNull apply both on persist/update and on load

from achilles.

doanduyhai avatar doanduyhai commented on August 15, 2024

Interestingly, @EmptyCollectionIfNull can be coded as a built-in interceptor that takes place before Bean Validation

from achilles.

vladimirdolzhenko avatar vladimirdolzhenko commented on August 15, 2024

I don't like to introduce one more annotation while @NotNull in this case means the same

from achilles.

doanduyhai avatar doanduyhai commented on August 15, 2024

Or I can make deserializing null into empty collections as default behavior in Achilles. This way, no need to introduce an extra annotation

from achilles.

vladimirdolzhenko avatar vladimirdolzhenko commented on August 15, 2024

My point is

  • default behavior in Achilles is null values for collections
  • if there is a @NotNull or @EmptyCollectionIfNull provides empty collection (new HashSet<> neither Collections.emptySet() )

from achilles.

doanduyhai avatar doanduyhai commented on August 15, 2024

Ok, I would go with @EmptyCollectionIfNull. @NotNull will force people to pull Bean Validation jar dependency even though they do not want to use Bean Validation

from achilles.

doanduyhai avatar doanduyhai commented on August 15, 2024

I've just check, the default behavior of the Java Driver is to return an empty collection or map if the value coming from Cassandra is null: https://github.com/datastax/java-driver/blob/2.0/driver-core/src/main/java/com/datastax/driver/core/ArrayBackedRow.java#L254

Since Achilles gets value from the Java Driver, there is nothing to do indeed ...

I've written an IT to confirm it:

  @Test
    public void should_test_not_null_collections() throws Exception {
        //Given
        CompleteBean entity = CompleteBeanTestBuilder.builder().id(10L).name("name").buid();
        manager.persist(entity);

        //When
        final Row row = manager.getNativeSession().execute("SELECT * FROM CompleteBean WHERE id=?", 10L).one();

        //Then
        final List<String> friends = row.getList("friends", String.class);
        assertThat(friends).isNotNull().isEmpty();

        final Set<String> followers = row.getSet("followers", String.class);
        assertThat(followers).isNotNull().isEmpty();

        final Map<Integer, String> preferences = row.getMap("preferences", Integer.class, String.class);
        assertThat(preferences).isNotNull().isEmpty();
    }

This issue can be closed.

from achilles.

vladimirdolzhenko avatar vladimirdolzhenko commented on August 15, 2024

I think the bug has to be reopened:

my code:
getPersistenceManager().typedQuery(MyEntity.class, "SELECT * from myentity;").get();

TypedQueryBuilder.get : 79
T entity = mapper.mapRowToEntityWithPrimaryKey(meta, row, propertiesMap, managed);

EntityMapper.mapRowToEntityWithPrimaryKey : 71
Object value = cqlRowInvoker.invokeOnRowForFields(row, pm);

RowMethodInvoker.invokeOnRowForFields : 41
if (row != null && !row.isNull(propertyName)) {

com.datastax.driver.core.ArrayBackedRow.isNull : 56
return isNull(metadata.getFirstIdx(name));

com.datastax.driver.core.ArrayBackedRow.isNull : 52
return data.get(i) == null;

based on the data it returns NULL -> therefore entity is NOT populated with non-null set/list/map

from achilles.

doanduyhai avatar doanduyhai commented on August 15, 2024

You're right, the check in done in Achilles

On Tue, Apr 29, 2014 at 10:31 AM, Vladimir Dolzhenko <
[email protected]> wrote:

I think that the bug has to be reopened:

TypedQueryBuilder.get : 79
T entity = mapper.mapRowToEntityWithPrimaryKey(meta, row, propertiesMap,
managed);

EntityMapper.mapRowToEntityWithPrimaryKey : 71
Object value = cqlRowInvoker.invokeOnRowForFields(row, pm);

RowMethodInvoker.invokeOnRowForFields : 41
if (row != null && !row.isNull(propertyName)) {

com.datastax.driver.core.ArrayBackedRow.isNull : 56
return isNull(metadata.getFirstIdx(name));

com.datastax.driver.core.ArrayBackedRow.isNull : 52
return data.get(i) == null;

based on the data it returns NULL -> therefore entity is NOT populated
with non-null set/list/map


Reply to this email directly or view it on GitHubhttps://github.com//issues/53#issuecomment-41652516
.

from achilles.

vladimirdolzhenko avatar vladimirdolzhenko commented on August 15, 2024

btw, I didn't find your IT in code - have you committed it ?

from achilles.

doanduyhai avatar doanduyhai commented on August 15, 2024

Nope, I did not commit the IT since it was for checking only. But I'll definitely add new IT to cover the @EmptyCollectionIfNull case

from achilles.

doanduyhai avatar doanduyhai commented on August 15, 2024

Done. Add unit and integration tests too

from achilles.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.