nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type InwardRegister - spring-data-cassandra

I receive the following error:
org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type InwardRegister
This is my code:
#Repository
public interface InwardRegisterRepository extends ReactiveSortingRepository<InwardRegister,Long> {
#AllowFiltering
Flux<InwardRegister> findByDateTime(Date dateTime);
#AllowFiltering
Flux<InwardRegister> findBySenderOrderById(String sender);
}
#Service
public class InwardRegisterService {
#Autowired
InwardRegisterRepository inwardRegisterRepository;
public Mono<InwardRegister> create(InwardRegister inwardRegisterRecord) {
return inwardRegisterRepository.save(inwardRegisterRecord);
}
public Mono<InwardRegister> read(Long id){
return inwardRegisterRepository.findById(id);
}
public Flux<InwardRegister> readAll(){
return inwardRegisterRepository.findAll(Sort.by("ASI","id"));
}
}

Error Looks like your bean InwardRegister.class does not contain #Id javax.persistence annotation for please provide Entity class details to check further..

Related

Mockito, channel output of void function to System.out?

I have this logging class in my legacy application,
I am trying to Mock it for testing and output all messages from "WriteLog" method to System.out
This is the class
public abstract class LoggingServicesWorker {
public abstract void WriteLog(ELogLevel arg0, int arg1,String arg2,String arg3);
}
This is what I did so far,
I am not sure how to write to System.out
Also How to tell the first argument to have any enum type (ELogLevel)?
LoggingServicesWorker logger = mock(LoggingServicesWorker.class);
Mockito.doNothing().when(logger).WriteLog(ELogLevel.DEBUG,anyInt(),anyString(),Mockito.eq(anyString()));
You can use Mockito#doAnswer for executing side-effects:
doAnswer((invocation -> {
System.out.println(Arrays.toString(invocation.getArguments()));
return null;
}))
.when(worker)
.WriteLog(any(ELogLevel.class), anyInt(), anyString(), anyString());
Standalone class with the default behavior for return values (and usable with Java 7):
class PrintArgsToStdoutAnswer implements Answer<Object> {
private final ReturnsEmptyValues defaultReturn = new ReturnsEmptyValues();
#Override
public Object answer(InvocationOnMock invocation) throws Throwable {
System.out.println(Arrays.toString(invocation.getArguments()));
return defaultReturn.answer(invocation);
}
}
...and used in the test method:
doAnswer(new PrintArgsToStdoutAnswer())...

failed to lazily initialize a collection of role for a Map<Integer,String>

I have the follwoing entity
#Entity
#Table(name = "Parent")
public class Parent {
#ElementCollecion(fetch = FetchType.Eager)
Map<Integer,String> myMap;
public Map<Integer,String> getMyMap() {
return this.myMap;
}
}
Then somewhere in the code I have the follwoing
parent.getMyMap().get(someKey);
This line consistently throws a LazyInitialization exception. I have set the fetchType to eagerly load the collection so that the values are available at all times. What have I missed> Is it because of the access of the getMap.GetKey call?
I dont see any more information in the debug logs or anywhere else.
Generally the #ElementCollection is accompanied with a #CollectionTable along with #MapKeyColumn & #Column annotation to provide the table and values to load the content for the map.
#Entity
#Table(name = "Parent")
public class Parent {
#ElementCollection(fetch = FetchType.Eager)
#CollectionTable(name="example_attributes", joinColumns=#JoinColumn(name="example_id"))
#MapKeyColumn(name="name")
#Column(name="value")
Map<Integer,String> myMap;
public Map<Integer,String> getMyMap() {
return this.myMap;
}
}

Can I use any kind of method within a get and set accessor of a public property of a public class

I have a class BaseTableC inheriting from an interface I_BaseTableC ,both having a set of public properties.
Can I access the "altered"(altered by MethodAlterValue) value provided by get/set accessors by reference of BaseTableC.Property1 or BaseTableC.I_BaseTableC I_refernce as I_reference.Property1?
As of now, I don't get an error, but the Property1 never shows the altered value as provided by the get/set accessor.
public class BaseTableC : IBaseTableC
{
public string Property1
{
get { return MethodOriginalValue(_Property1); }
set
{
if (! String.IsNullOrEmpty(value))
_Property1 = MethodAlterValue(value.TrimEnd());
else
_Property1 = value;
}
}
}

AutoMapper ConstructServicesUsing is ignored

I have a Person and a PersonViewModel. I created a map from Person => PersonViewModel. The problem is that PersonViewModel's only constructor needs an argument (it has a dependency that I want to be injected) and AutoMapper is complaining because it says it needs a parameterless constructor.
To fix it, I used the ConstructServicesUsing method, but I haven't been successful with it :(
To illustrate the case, I created a test for you to see what I'm doing. It's pretty simple:
[TestMethod]
public void TestConstructServicesUsing()
{
Mapper.Initialize(configuration =>
{
configuration.ConstructServicesUsing(FactoryMethod);
configuration.CreateMap<Person, PersonViewModel>();
});
Mapper.AssertConfigurationIsValid();
var person = new Person();
var personViewModel = Mapper.Map<Person, PersonViewModel>(person);
}
private object FactoryMethod(Type type)
{
throw new NotImplementedException();
}
}
The rest of the code is the classes and interface definitions. They are almost empty.
public class SomeyDependency : ISomeDependency
{
}
public class PersonViewModel
{
private readonly ISomeDependency service;
public PersonViewModel(ISomeDependency service)
{
this.service = service;
}
public string Name { get; set; }
}
public class Person
{
public string Name { get; set; }
}
public interface ISomeDependency
{
}
As you see, I provide AutoMapper with a FactoryMethod, but it never get called.
When it reaches the last line of the test (Mapper.Map<...>()) it throws an excepton saying:
AutoMapper.AutoMapperMappingException:
Mapping types:
Person -> PersonViewModel
MappingWithContainerTests.Person -> MappingWithContainerTests.PersonViewModel
Destination path:
PersonViewModel
Source value:
MappingWithContainerTests.Person ---> System.ArgumentException: Type needs to have a constructor with 0 args or only optional args
Parameter name: type
What's the problem?
Why isn't the FactoryMethod being called?
As #khorvat mention where is missing .ConstructUsingServiceLocator(), for concrete mapping.
Also you can set constructor directly by
.ConstructUsing(source => Method(source.anySourceOptions))
Or as exception said:
PersonViewModel, must have a constructor with 0 args or only optional
args. You have only one constructor with 1 not optional argument
you may create one more constructor without args:
public PersonViewModel()
{
this.service = new SomeDependency();
}
I'm using .NET Core 3.1 and Automapper.Extensions.Microsoft.DependencyInjection.
This does not work for me (Same error as yours):
public class AutoMapping : Profile
{
public AutoMapping()
{
CreateMap<Context, MainViewModel>()
.ReverseMap()
.ConstructUsingServiceLocator();
}
}
But this does work:
public class AutoMapping : Profile
{
public AutoMapping()
{
CreateMap<Context, MainViewModel>()
.ConstructUsingServiceLocator()
.ReverseMap();
}
}
I still do not fully understand the cause.

Why does JAXB call getter during unmarshalling

I was surprised to see the following stack trace during JAXB unmarshalling:
[#|2013-02-05T18:59:27.551-0500|SEVERE|glassfish3.1.2|ConfigurationService|_ThreadID=82;_ThreadName=Thread-2;|Exception processing C:\glassfish3\glassfish\domains\domain1\config\myConfig.xml : #NotNull method com/foo/services/config/Config.getBars must not return null
java.lang.IllegalStateException: #NotNull method com.foo.services.config.Config.getBars must not return null
at com.foo.services.Config.getBars(Config.java:222)
at com.foo.services.Config$JaxbAccessorM_getBars_setBars_java_util_List.get(MethodAccessor_Ref.java:56)
at com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.startPacking(Lister.java:294)
at com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.startPacking(Lister.java:269)
at com.sun.xml.bind.v2.runtime.unmarshaller.Scope.start(Scope.java:142)
at com.sun.xml.bind.v2.runtime.property.ArrayERProperty$ItemsLoader.startElement(ArrayERProperty.java:119)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:501)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:480)
at com.sun.xml.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:102)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:150)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2715)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:218)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:190)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:172)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:177)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:186)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:204)
The getter is annotated with org.jetbrains.annotation.NotNull with the intent that it should be marked to not return NULL because the getter is also annotated with #XmlElementRef(required = true). So bascially the #NotNull was put there to tell clients hey this should never be null because its a required element in the XML file being unmarshalled and as such either the parsing will fail because its missing or its going to be there. More info on #NotNull can be found here.
The property associated with the getter in this case is a List<Bar> which is not initialized by the class to anything as its expected that the unmarshalling process will do so.
In any case I am seeing that if the parsing fails during unmarshalling JAXB calls the getter and this trips the #NotNull which generates the above exception.
Can anyone shed light on this behavior? Thanks,
-Noah
A JAXB (JSR-222) implementation by default treats public properties as mapped. The reason it calls get on your List property is to see if you a value has been pre-initialized.
Scenario #1
JAXB will call getBars() to see if a collection has already been created, this will return null. Since null was returned JAXB will create an instance of java.util.ArrayList which will be set via setBars.
public class Foo {
private List<Bar> bars;
public List<Bar> getBars() {
return bars;
}
public void setBars(List<Bar> bars) {
this.bars = bars;
}
}
Scenario #2
JAXB will call getBars() to see if a collection has already been created, this will return an instance of LinkList. Since null was not returned JAXB will use the instance of List returned from the get method.
public class Foo {
private List<Bar> bars = new LinkedList<Bar>();
public List<Bar> getBars() {
return bars;
}
public void setBars(List<Bar> bars) {
this.bars = bars;
}
}
Scenario #3
If you would rather that JAXB use the fields instead of the properties, then you can specify #XmlAccessorType(XmlAccessType.FIELD) on the class or package (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).
#XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
private List<Bar> bars;
public List<Bar> getBars() {
return bars;
}
public void setBars(List<Bar> bars) {
this.bars = bars;
}
}

Resources