How to get nested ValueObject changes? - javers

I have the following hierarchy of classes:
class Incident {// Id => Entity
#Id
String id
List<Participant> participants
List<RealEstateProperty> realEstateProperties
}
where
class Participant {// No id => by javers terms - ValueObject
EnclosedContact contact
}
class EnclosedContact {// No id => by javers terms - ValueObject
String name
}
class RealEstateProperty {// No id => by javers terms - ValueObject
List<CadastralSection> cadastralSections
}
class CadastralSection {// No id => by javers terms - ValueObject
String sectionId
}
I have written the following test (in groovy):
def "Querying Javers Repository for participants changes works correctly"() {
given:
(1..3).each {
javers.commit("author", new Incident(
id: it,
participants: [
new Participant(contact: new EnclosedContact(id: 20 + it))
]
))
}
when:
def snapshots = javers.findSnapshots(QueryBuilder.byValueObjectId(1, Incident.class, "contact").build())
then:
assert snapshots.size() == 1
}
The result of this test is:
JaversException: PROPERTY_NOT_FOUND property 'contact' not found in class 'Incident'
Trying to get the changes this way
def snapshots = javers.findSnapshots(QueryBuilder.byValueObjectId(1, Incident.class, "participants/0/contact").build())
returns empty list.
Does Javers support selecting for changes on nested ValueObjects?

in JaVers 1.6.2 there is a basic support for nested ValueObjects queries (undocumented yet). Your query should work for data persisted by this JaVers version. For example:
def "should query for changes on nested ValueObjects stored in a list"(){
given:
def user = new DummyUserDetails(
id:1,
addressList: [new DummyAddress(networkAddress: new DummyNetworkAddress(address: "a"))])
javers.commit("author", user)
user.addressList[0].networkAddress.address = "b"
javers.commit("author", user)
when:
def changes = javers.findChanges(QueryBuilder.byValueObjectId(1, DummyUserDetails,
"addressList/0/networkAddress").build())
then:
changes.size() == 1
changes[0].left == "a"
changes[0].right == "b"
}

I created a generic nested object comparator, hope it helps.
https://gist.github.com/hank-cp/3db40faed1dd9f02ababd86c2c9eaf8d
Registers it like this way:
NestedObjectComparator productRootComparator =
new NestedObjectComparator<POProductRoot>() {};
sharedJavers = JaversBuilder.javers()
.registerValue(POProductRoot.class)
.registerCustomComparator(productRootComparator, POProductRoot.class).build();
productRootComparator.setJsonConverter(sharedJavers.getJsonConverter());
Then your will get the changes result in MapChange format.

Since JaVers 2.1, there is a new filter for this type of queries - child-value-objects-filter

Related

Springboot Mongo reactive repository unable to update nested list

I wanted to update a nested list but I experience a strange behavior where I have to call method twice to get it done...
Here is my POJO:
#Document(collection = "company")
data class Company (
val id: ObjectId,
#Indexed(unique=true)
val name: String,
val customers: MutableList<Customer> = mutableListOf()
//other fields
)
Below is my function from custom repository to do the job which I based on this tutorial
override fun addCustomer(customer: Customer): Mono<Company> {
val query = Query(Criteria.where("employees.keycloakId").`is`(customer.createdBy))
val update = Update().addToSet("customers", customer)
val upsertOption = FindAndModifyOptions.options().upsert(true)
//if I uncomment below this will work...
//mongoTemplate.findAndModify(query, update, upsertOption, Company::class.java).block()
return mongoTemplate.findAndModify(query, update, upsertOption, Company::class.java)
}
In order to actually add this customer I have to either uncomment the block call above or call the method two times in the debugger while running integration tests which is quite confusing to me
Here is the failing test
#Test
fun addCustomer() {
//given
val company = fixture.company
val initialCustomerSize = company.customers.size
companyRepository.save(company).block()
val customerToAdd = CustomerReference(id = ObjectId.get(),
keycloakId = "dummy",
username = "customerName",
email = "email",
createdBy = company.employees[0].keycloakId)
//when, then
StepVerifier.create(companyCustomRepositoryImpl.addCustomer(customerToAdd))
.assertNext { updatedCompany -> assertThat(updatedCompany.customers).hasSize(initialCustomerSize + 1) }
.verifyComplete()
}
java.lang.AssertionError:
Expected size:<3> but was:<2> in:
I found out the issue.
By default mongo returns entity with state of before update. To override it I had to add:
val upsertOption = FindAndModifyOptions.options()
.returnNew(true)
.upsert(true)

MongoDB embedded Document Array: Get only one embedded document with a spezific attribute

I want to get one Embedded Document with a specific field (version) from an array with mongodb and spring boot.
This is the data structure:
{
"_id": 5f25882d28e40663719d0b52,
"versions": [
{
"versionNr": 1
"content": "This is the first Version of some Text"
},
{
"versionNr": 2
"content": "This is the second Version of some Text"
},
...
]
...
}
Here are my entities:
#Data
#Document(collection = "letters")
public class Letter {
#Id
#Field("_id")
private ObjectId _id;
#Field("versions")
private List<Version> versions;
}
//There is no id for embedded documents
#Data
#Document(collection = "Version")
public class Version{
#Field("content")
private String content;
#Field("version")
private Long version;
}
And this is the query that doesn't work. I think the "join" isn't correct. But can't figure out the right way.
public Optional<Version> findByIdAndVersion(ObjectId id, Long version) {
Query query = new Query(Criteria.where("_id").is(id).and("versions.version").is(version));
return Optional.ofNullable(mongoTemplate.findOne(query,Version.class,"letters"));
}
}
EDIT: This is a working Aggregation, I'm sure it isn't a pretty solution but it works
#Override
public Optional<Version> findByIdAndVersion(ObjectId id, Long version) {
MatchOperation match = new MatchOperation(Criteria.where("_id").is(id).and("versions.version").is(version));
Aggregation aggregate = Aggregation.newAggregation(
match,
Aggregation.unwind("versions"),
match,
Aggregation.project()
.andInclude("versions.content")
.andInclude("versions.version")
);
AggregationResults<Version> aggregateResult = mongoTemplate.aggregate(aggregate, "letters", Version.class);
Version version = aggregateResult.getUniqueMappedResult();
return Optional.ofNullable(mongoRawPage);
}
Query query = new Query(Criteria.where("_id").is(id).and("versions.version").is(version));
return Optional.ofNullable(mongoTemplate.findOne(query,Version.class,"letters"));
You are querying the Letter document but your entity class is specified as Version.class, since findOne from MongoDB doesn't return the subdocument by itself but rather the whole document, you need to have Letter.class as return type and filter (project) what fields to get back. So you can either project the single version subdocument that you want to receive, like so:
Query query = new Query()
.addCriteria(Criteria.where("_id").is(id).and("versions.version").is(version))
.fields().position("versions", 1);
Optional.ofNullable(mongoTemplate.findOne(query, Letter.class))
.map(Letter::getVersions)
.findFirst()
.orElse(null);
or use aggregation pipeline:
newAggregation(
Letter.class,
match(Criteria.where("_id").is(id)),
unwind("versions"),
replaceRoot("versions"),
match(Criteria.where("version").is(version))),
Version.class)
Note -- I typed this on a fly.

Grails unique constraint not working on multiple fields

I've read a lot about uniqueness and constraints in Grails (but maybe not enough)
I can't make the unique constraint to work on multiple fields as explained here:
http://grails.org/doc/1.3.7/ref/Constraints/unique.html
(I'm using grails 1.3.9)
I have 2 domain classes:
class Dog {
static constraints = {
humanSsn(unique: ['name', 'breed'])
//I also tried with just 2 fields, didn't work either.
}
Integer humanSsn
String name
String breed
}
class Human {
static constraints = {
ssn(unique: true)
}
Integer ssn
String name
}
It is a legacy DB, so I cant modify the tables.
When I save a Human, I (just to test) save two dogs with the same name, breed and humanSsn
def humanoInstance = new Humano(params)
if (humanoInstance.save(flush: true)) {
def newDog = new Dog()
def newDogTwo = new Dog()
newDog.name = "n1"
newDog.breed = "b1"
newDog.humanSsn = humanInstance.ssn
println newDog.validate()
println newDog.getErrors()
newDog.save(failOnError:true)
newDogTwo.name = "n1"
newDogTwo.breed = "b1"
newDogTwo.humanSsn = humanInstance.ssn
println newDogTwo.validate()
println newDogTwo.getErrors()
newDogTwo.save(failOnError:true)
}
But it saves anyway the 2 dogs without complaining nor throwing any errors.
true
org.springframework.validation.BeanPropertyBindingResult: 0 error
true
org.springframework.validation.BeanPropertyBindingResult: 0 error
What am I doing wrong?
Thanks in advance.
it may be due to validation works on database level
and newDog.save(failOnError:true) doesnot save dog object immediately
have you try
newDog.save(flush:true)
for first dog and then
newDogTwo.save(failOnError:true)
it should work

How do I sort by a property on a nullable association in Grails?

I'm trying to sort a table of data. I have the following domain (paraphrased and example-ified):
class Car {
Engine engine
static constraints = {
engine nullable: true // poor example, I know
}
}
class Engine {
String name
}
Here's the controller action that's handling the sort:
def myAction = {
def list = Car.findAll(params)
render(view: 'list', model: [list: list])
}
I provision some data such that there are several Cars, some with null engines and others with engines that are not null.
I attempt the following query:
http://www.example.com/myController/myAction?sort=engine.name&order=asc
The results from the query only return Car entries whose engine is not null. This is different from the results that would be returned if I only queried the association (without its property):
http://www.example.com/myController/myAction?sort=engine&order=asc
which would return all of the Car results, grouping the ones with null engines together.
Is there any way that:
I can get the query that sorts by the association property to return the same results as the one that sorts by only the association (with the null associations grouped together)?
I can achieve those results using the built-in sorting passed to list() (i.e. without using a Criteria or HQL query)
You need to specify LEFT_JOIN in the query, try this:
import org.hibernate.criterion.CriteriaSpecification
...
def list = Car.createCriteria().list ([max:params.max?:10, offset: params.offset?:0 ]){
if (params.sort == 'engine.name') {
createAlias("engine","e", CriteriaSpecification.LEFT_JOIN)
order( "e.name",params.order)
} else {
order(params.sort, params.order)
}
}
Remember to put engine.name as the property to order by in your list.gsp
<g:sortableColumn property="engine.name" title="Engine Name" />

Can ExecuteQuery return a DBML generated class without having it fetch all the information for that class?

I have a couple of DBML generated classes which are linked together by an id, e.g.
ClassA {
AID,
XID,
Name
}
ClassB {
AID,
ExtraInfo,
ExtraInfo2
}
In using something like db.ClassAs.Where(XID == x) and iterating through that result,
it ends up executing a query for each of the ClassAs and each of ClassBs, which is slow.
Alternatively, I've tried to use ExecuteQuery to fetch all the info I care about and have that return a ClassA. In iterating over that I end up with it doing the same, i.e. doing alot of individual fetches vs. just 1. If I store it in a ClassC (that is not associated with a DB entity) which has the fields of interest of both ClassA and ClassB, this query is much faster, but it's annoying b/c I just created IMO an unnecessary ClassC.
How can I still use ClassA, which associates to ClassB, and still use ExecuteQuery to run 1 query vs. A*B number of queries?
If you have associations you shouldn't need to use ExecuteQuery().
Here's an example using some imaginary Book Library context and anonymous types for the result:
var results =
Books
.Where(book => book.BookId == 1)
.Select(book =>
new
{
book.Name,
AuthorName = book.Author.Name, //Is a field in an associated table.
book.Publisher, //Is an associtated table.
});
EDIT: without anon types
var results =
Books
.Where(book => book.BookId == 1)
.Select(book =>
new BookResult()
{
BookName = book.Name,
AuthorName = book.Author.Name, //Is a field in an associated table.
Publisher = book.Publisher, //Is an associtated table.
});

Resources