Grails multi datasource domain issue - database

I have a project which tables spread between 2 datasources.
I'm configuring the code to access table as per 3.3.6 topic in grails documentations
http://grails.org/doc/2.0.0.M2/guide/conf.html#dataSourcesAndEnvironments
Everything seems to be ok, but I got the following error
Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: Association references unmapped class: br.com.fisgo.Provider
Caused by MappingException: Association references unmapped class: br.com.fisgo.Provider
Any idea on why do I get this error?
Regards.
I'll try it out.
It won't be that simple because Company domain links back to Provider
It will just require more efort
class Company {
String name
String cnpj
String email
Address address
Phone phone
String registration
String source
Set provider = new HashSet<Provider>()
static hasMany = [provider: Provider]

You should use newer docs, e.g. http://grails.org/doc/latest/guide/conf.html#dataSourcesAndEnvironments
It looks like you're trying to link across datasources. This isn't possible since each DataSource has a separate SessionFactory, and they cannot work directly together. The same problem happens when you use Hibernate and a NoSQL GORM plugin.
You can mimic it easily enough though. Given a domain class Foo that needs a reference to Provider, you can persist the foreign key and look it up on-demand (and this is really what Hibernate does for you when you join between two domain classes):
class Foo {
Long providerId
Provider getProvider() {
providerId ? Provider.get(providerId) : null
}
void setProvider(Provider provider) {
providerId = provider.id
}
static transients = ['provider']
}
Since Groovy treats getter/setter pairs as a property, you would use it like a "real" link:
def foo = ...
def bar = foo.provider.bar

Related

ADF : Null pointer when accessing application module in Managed Bean

I am accessing the applyViewCriteria method written in VoImpl class from the managed bean. The code snippet is something like:
EmployeeAppModuleImpl appModule= new EmployeeAppModuleImpl();
ViewObject vo = appModule.findViewObject("EmployeesView");
I have the EmployeeAppModuleImpl class in which the EmployessView accessor methods are generated.
The "appModule" returned is null. I seem to be missing something important.
Please help!

Spring Rest error the second time that saves Object with child properties that references the same father the second time: Already had POJO for id

I'm trying to update an object using REST controller in Spring 3,Jackson 4.0, using the PUT method.
We have a Container class with 4 properties one to many relationships to Container.
#JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="#Id",scope = Container.class)
public class Container implements Comparable, Serializable {
private int containerId;
.
.
.
private Set<Container> containers;
private Collection<ImagePerContainer> imageControls;
private Collection<TextControl> textControls;
private Collection<PromoControl> promoControls;
private Collection<WebSource> webSourceControls;
}
Each one of this Objects (childs) have a reference to Container (father), like this:
#JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="#Id",scope = TextControl.class)
public class TextControl implements Serializable {
.
.
.
private int textControlId;
private String textControlName;
private Container container;
.
.
.
}
Im trying to update the object when my angularjs app modifes the model using $resources PUT request. The first time it saves succesfully, but the second time I get a 400 response from the server, which logs this error:
HandlerMethod details:
Controller [com.bamboo.catW3.web.json.ContainerController]
Method [public org.springframework.http.ResponseEntity<com.bamboo.catW3.domain.Container> com.bamboo.catW3.web.json.ContainerController.updateContainer(int,com.bamboo.catW3.domain.Container)]
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Already had POJO for id (java.util.UUID) [com.fasterxml.jackson.annotation.ObjectIdGenerator$IdKey#8adc3c85] (through reference chain: com.bamboo.catW3.domain.Container["#Id"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Already had POJO for id (java.util.UUID) [com.fasterxml.jackson.annotation.ObjectIdGenerator$IdKey#8adc3c85] (through reference chain: com.bamboo.catW3.domain.Container["#Id"])
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:171)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:163)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:135)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:180)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:95)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
If I update another Container it works also the first time, but if I modified again and try to update it gives me the same 400 error. It doesn't even enter the controller. If I restart the server, then it lets me update the object one time, and it stops working again.
Why does it work the first time only ? How can I fix this ?
I faced the same problem, and just found this link :
https://github.com/FasterXML/jackson-databind/issues/499
It said that it was a bug in jackson 2.4.0 that was fixed in 2.4.1 and later.
Just update your jackson library to the latest stable release.

Entity Framework 5 Table Relationship

I use database first. In the auto-generated EF5 code:
Account has ClientID(FK)
Client has AddressID(FK)
Address has public List<EFClient> Clients { get; set; } (i did not specify this in DB but ef5 auto generated it)
When I serialize Address object, it throws exception "there is a circular reference" because the client collection in the address also cotains same address again
What can I do in this situation?
How can I not let EF5 auto-generate that Clients collection?
Thanks in advance!
I don't think you actually want to stop EF from auto-generating this navigation property, as it will affect lots of places. What you can do is simply remove the property that it generated from the model, which will remove the property from the class.

Error deserializing read-only property with Jackson

In My CXF-based REST layer I am using Jackson for seializing/deserializing Groovy objects. The issue I am experiencing deals with deserializing a property that does not have a setter method. There is a domain object Dashboard with getGroups() method returning a list of Group objects. Upon serialization this object is properly converted to JSON with "group" attribute. When I send the object for update from JavaScript, JSON still has the "group" attribute. Since the property is read-only on the domain object I would like to simply ignore the "group" property when deserializing JSON.
Since I am using Jackson mix-ins, I tried various combination of #JsonIgnore, #JsonGetter and #JsonProperty annotations - all to no avail. If the property is available upon serialization, I get the error below in deserialization. I can clean the JSON object in JavaScript by removing the "group" attribute, bit I would like to find a server-side solution.
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Problem deserializing 'setterless' property 'groups': get method returned null (through reference chain: org.ozoneplatform.commons.server.domain.model.DashboardTemplate["groups"])
at com.fasterxml.jackson.databind.deser.impl.SetterlessProperty.deserializeAndSet(SetterlessProperty.java:114)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:198)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:577)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:393)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:289)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1169)
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:625)
at com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:448)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1038)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:614)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:578)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:238)
How can I tell Jackson to ignore a read-only property on deserialization?
Thank you,
Michael
After many fruitless hours, I have finally found the magic combination of spells that addresses such a seemingly trivial issue. In the mixin I had to create this combination of annotations:
#JsonIgnore
abstract Set<Group> groups
#JsonProperty
abstract Set<Group> getGroups()
#JsonIgnore
abstract void setGroups(Set<Group> groups)
On top of that I had to add two configuration parameters to the ObjectMapper:
mapper.configure(MapperFeature.USE_GETTERS_AS_SETTERS, false)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
Isn't there a simpler way to achieve the same?
Michael
I had the same issue. Solution was to give the getter the correct name:
List list...
setList(...)
was correct, but my getter was
setProductList() which produced the "setterLess" error. changing it to:
setList(...) resolved the issue
I just ran into the same problem, and my solution was to create a private, no-op setter:
public class MyFoo {
public String getMyStr() {
return "hello, world";
}
private void setMyStr(String ignored) {}
}
Making setMyStr private prevents me from accidentally trying to call it from my code, but Jackson still finds it and invokes it. Little does jackson know -- or care -- that invoking it does nothing.

XmlSerializer stopped working after updates

I'm using XmlSerializer. I've had no problems with it until now. I updated Silverlight from 4 to 5 and at the same time also updated the WCF RIA Services from v1 SP1 to v1 SP2. Now the following line gives me an error.
XmlSerializer s = new XmlSerializer(typeof(MyCustomObject));
The error is:
System.InvalidOperationException: System.ServiceModel.DomainServices.Client.EntityConflict cannot be serialized because it does not have a parameterless constructor.
The object I'm using (MyCustomObject in the sample) has not changed in any way so I'm starting to think it's either SL5 or the new RIA Services that is breaking my code. I didn't find any breaking changes document or mentions that this could happen. I don't know why it has a problem with EntityConflict since I'm not using any entities within my object.
Has anyone seen an error like this and/or know how to solve it?
UPDATE!
The final property that the error message says before EntityConflict is an Entity. I think that makes a difference but it has been working before. I'd also like to know why the serializer already tries to serialize the object in the constructor?
public static XmlSerializer GetEntityXmlSerializer<TEntity>()
where TEntity : Entity
{
XmlAttributes ignoreAttribute = new XmlAttributes()
{
XmlIgnore = true,
};
// use base class of Entity,
// if you use type of implementation
// you will get the error.
Type entityType = typeof(Entity);
var xmlAttributeOverrides = new XmlAttributeOverrides();
xmlAttributeOverrides.Add(entityType, "EntityConflict", ignoreAttribute);
xmlAttributeOverrides.Add(entityType, "EntityState", ignoreAttribute);
return new XmlSerializer(typeof(TEntity), xmlAttributeOverrides);
}
I am not sure why this would be happening, RIA Services entities are not XmlSerializable objects and the entities themselves are not decorated with the [Serializable] attribute. Have you added partial classes on the client side which decorate the entities with [Serializable] or modified the code generation in some way?
I got around this problem by using intermediary serializable POCO objects which were copies of my custom objects (which were inherited from Entity). The POCO objects did not inherit from Entity. I just updated their values from the original Entity objects. They then serialized quite nicely. Of course, when you de-serialize you need to update your Entity objects from the POCO objects.

Resources