Serializable[] attribute in Object not stored in db4o - java

I ran in a strange behavior of db4o. When I persist an Object (implementing Serializable) with an attribute of Serializable[], the Array is only returned once from the store correctly then ever after only an Array with null elements.
I use db4o 7.12.
Edit
This is the POJO:
public class ResponseRowWrapper implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private long uuid;
private long responseUuid;
private Serializable[] row;
private long timestamp;
And this the config in spring:
<property name="transparentActivation" value="true" />
<property name="transparentPersistence" value="true" />
<property name="configurationCreationMode" value="NEW" />
<property name="lockDatabaseFile" value="false" />
<property name="callConstructors" value="true" />
<property name="exceptionsOnNotStorable" value="true" />
and in in the db4o Config Object:
configuration.common().objectClass(ResponseRowWrapper.class).cascadeOnUpdate(true);

Try passing the db4o configuration object a cascadeOnActivate(true) (that causes a cascaded load) or set a higher activation depth in db4o.

I solved it, but I can't tell what was wrong with a Serializable[] but it is not working, I need to use Object[] and just care by myself that there are only Serializables inside.

Related

Scope of bean and reference bean

In a spring application,
Say Bean person is of scope prototype
and Bean address is of scope singleton
Class person
{
string name;
int age;
int rollno;
Address address;
// Getters & Setters
}
Class Address
{
int door_no;
string street_name;
// Getters & Setters
}
It is obvious that a new instance will be created for Person each time when requested.
now will a new instance be created for every request for Address or since it is referenced from person it will have the scope - prototype?
In sringBean.xml
<bean id="studentBeanId" class="com.saro.corespring.autowire.Person"
autowire="byName" scope="prototype">
<property name="name" value="saro" />
<property name="age" value="25" />
<property name="roll_no" value="101" />
</bean>
<bean id="addressBean" class="com.saro.corespring.autowire.Address" autowire="byType" scope="singleton">
<property name="door_no" value="10" />
<property name="street_name" value="Street Name" />
</bean>

Reading 2 property files having same variable names in Spring

I am reading property files using below entry in my Spring xml.
<context:property-placeholder
location="classpath:resources/database1.properties,
classpath:resources/licence.properties"/>
I am injecting this values in variable using xml entry or using #Value annotation.
<bean id="myClass" class="MyClass">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="name" value="${database.name}" />
</bean>
I want to add a new property file(database2.properties) which has few same variable names as of database1.properties.
database1.properties:
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://192.168.1.10/
database.name=dbname
database2.properties:
database.url=jdbc:mysql://192.168.1.50/
database.name=anotherdbname
database.user=sampleuser
You can see few property variables have same name like database.url, database.name in both the property files.
Is it possible to inject database.url of database2.properties?
Or I have to change variable names?
Thank you.
You can do it by configuring two PropertyPlaceholderConfigurer. Usually there's only one instance that serves out all the properties, however, if you change the placeholderPrefix you can use two instances, something like
<bean id="firstPropertyGroup" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:resources/database1.properties,
classpath:resources/licence.properties" />
<property name="placeholderPrefix" value="${db1."/>
</bean>
<bean id="secondPropertyGroup" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:resources/database2.properties" />
<property name="placeholderPrefix" value="${db2."/>"
</bean>
Then you would access your properties like ${db1.database.url} or ${db2.database.url}
There might be a solution, similar to what's that you want to achieve. Check the second answer to this question: Multiple properties access. It basically explains what to do in order to access the properties of the second file by using another expression, which is defined by you.
Otherwise, the simplest solution would be just changing the key values (the variable names).
You will sooner or later switch to Spring Boot. So with Spring Boot you can do have such POJO:
public class Database {
#NotBlank
private String driver;
#NotBlank
private String url;
#NotBlank
private String dbname;
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDbname() {
return dbname;
}
public void setDbname(String dbname) {
this.dbname = dbname;
}
}
and use #ConfigurationProperties to fill it:
#Bean
#ConfigurationProperties(locations="classpath:database1.properties", prefix="driver")
public Database database1(){
return new Database();
}
#Bean
#ConfigurationProperties(locations="classpath:database2.properties", prefix="driver")
public Database database2(){
return new Database();
}
Downside of this is that it's mutable. With Lombok library, you can eliminate nasty getters and setters.

SpringBatch Jaxb2Marshaller: different name of class and xml attribute

I try to read an xml file as input for spring batch:
Java Class:
package de.example.schema.processes.standardprocess;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "Process", namespace = "http://schema.example.de/processes/process", propOrder = {
"input"
})
public class Process implements Serializable
{
#XmlElement(namespace = "http://schema.example.de/processes/process")
protected ProcessInput input;
public ProcessInput getInput() {
return input;
}
public void setInput(ProcessInput value) {
this.input = value;
}
}
SpringBatch dev-job.xml:
<bean id="exampleReader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="step">
<property name="fragmentRootElementName" value="input" />
<property name="resource"
value="file:#{jobParameters['dateiname']}" />
<property name="unmarshaller" ref="jaxb2Marshaller" />
</bean>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>de.example.schema.processes.standardprocess.Process</value>
<value>de.example.schema.processes.standardprocess.ProcessInput</value>
...
</list>
</property>
</bean>
Input file:
<?xml version="1.0" encoding="UTF-8"?>
<process:process xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:process="http://schema.example.de/processes/process">
<process:input>
...
</process:input>
</process:process>
It fires the following exception:
[javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schema.example.de/processes/process", local:"input"). Expected elements are <<{http://schema.example.de/processes/process}processInput>]
at org.springframework.oxm.jaxb.JaxbUtils.convertJaxbException(JaxbUtils.java:92)
at org.springframework.oxm.jaxb.AbstractJaxbMarshaller.convertJaxbException(AbstractJaxbMarshaller.java:143)
at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:428)
If I change to in xml it work's fine. Unfortunately I can change neither the xml nor the java class.
Is there a possibility to make Jaxb2Marshaller map the element 'input' to the class 'ProcessInput'?
I don't believe JAXB allows this. JAXB is a binding API, so it doesn't provide much in the way of customization. That being said, you can use XStream and provide aliases for what you need, allowing you to customize the mapping of XML to object however you want. You can see an XStream example here: https://github.com/spring-projects/spring-batch/blob/master/spring-batch-samples/src/main/resources/jobs/iosample/xml.xml

how to inject map type object in spring 3.0

In DAO:
private Map<Integer,String> departments = new LinkedHashMap<Integer, String>();
#Override
public List<DepartmentEntity> getAllDepartments() {
return this.sessionFactory.getCurrentSession().createQuery("from DepartmentEntity de order by LOWER(de.departmentname)").list();
}
#Override
public Map<Integer, String> loadDepartments() {
departments.clear();
for (DepartmentEntity de : getAllDepartments())
departments.put(de.getDepartmentid(), de.getDepartmentname());
return departments;
}
Its Working fine, but in spring creation of objects manually its bad code
private Map<Integer,String> departments;
So, how to inject map object of LinkedHashMap type from out side in my case ?.
I tried but i got exceptions like null pointer exception
Please any one help me..
<util:map id="myMap" map-class="java.util.LinkedHashMap" key-type="java.lang.Integer" value-type="java.lang.String"/>
<bean id="departmentDAOImpl" class="com.leadwinner.infra.assets.dao.DepartmentDAOImpl">
<property name="departments" ref="myMap"></property>
</bean>
You can do something like below:
eg.
class A{
private B b;
public setB(B b){
this.b = b;
}
public Map getMapFromA(){
return b.getMap();
}
}
class B{
private Map tmp;
public void setMap(HashMap t){
tmp.putAll(t);
}
public HashMap getMap(){
return tmp;
}
}
And in web.xml
<bean id="classB" class="default.B"/>
<bean id ="classA" class="default.A"/>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject"><ref local="classA"></property>
<property name="targetMethod"><value>setB</value></property>
<property name="arguments"><ref local="classB"/></property>
</bean>
Now spring beans are by default singleton scoped. So you can do the following.
function do(){
B b = ctx.getBean("classB");
b.setMap(someMap);
A a = ctx.getBean("classA");
a.getMapFromA();
}
I havent tried out the code but it will give you an idea I hope so. More details on MethodInvokingFactoryBean : here
And if you dont want to do it by Spring and if you want less efforts try using ThreadLocal to pass parameters.
Populate map in this way (using constructor injection):
<bean name="DAO" class="path.to.yourDAOClass">
<constructor-arg index="0">
<map>
<entry key="1" value="One" />
<entry key="2" value="Two" />
</map>
</constructor-arg>
<bean>
By default target class for <map /> is a LinkedHashMap, but you can change target class using a MapFactoryBean to construct your map object in this way by replace the <map /> tag with:
<bean class="org.springframework.beans.factory.config.MapFactoryBean">
<property name="targetMapClass">
<value>java.util.HashMap</value>
</property>
<property name="sourceMap">
<map>
<entry key="1" value="One" />
<entry key="2" value="Two" />
</map>
</property>
</bean>

No endpoint mapping found for..., using SpringWS, JaxB Marshaller

I get this error: No endpoint mapping found for [SaajSoapMessage {http://mycompany/coolservice/specs}ChangePerson]
Following is my ws config file:
<bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">
<description>An endpoint mapping strategy that looks for #Endpoint and #PayloadRoot annotations.</description>
</bean>
<bean class="org.springframework.ws.server.endpoint.adapter.MarshallingMethodEndpointAdapter">
<description>Enables the MessageDispatchServlet to invoke methods requiring OXM marshalling.</description>
<constructor-arg ref="marshaller"/>
</bean>
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPaths">
<list>
<value>org.company.xml.persons</value>
<value>org.company.xml.person_allextensions</value>
<value>generated</value>
</list>
</property>
</bean>
<bean id="persons" class="com.easy95.springws.wsdl.wsdl11.MultiPrefixWSDL11Definition">
<property name="schemaCollection" ref="schemaCollection"/>
<property name="portTypeName" value="persons"/>
<property name="locationUri" value="/ws/personnelService/"/>
<property name="targetNamespace" value="http://mycompany/coolservice/specs/definitions"/>
</bean>
<bean id="schemaCollection" class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection">
<property name="xsds">
<list>
<value>/DataContract/Person-AllExtensions.xsd</value>
<value>/DataContract/Person.xsd</value>
</list>
</property>
<property name="inline" value="true"/>
</bean>
I have then the following files:
public interface MarshallingPersonService {
public final static String NAMESPACE = "http://mycompany/coolservice/specs";
public final static String CHANGE_PERSON = "ChangePerson";
public RespondPersonType changePerson(ChangePersonType request);
}
and
#Endpoint
public class PersonEndPoint implements MarshallingPersonService {
#PayloadRoot(localPart=CHANGE_PERSON, namespace=NAMESPACE)
public RespondPersonType changePerson(ChangePersonType request) {
System.out.println("Received a request, is request null? " + (request == null ? "yes" : "no"));
return null;
}
}
I am pretty much new to WebServices, and not very comfortable with annotations. I am following a tutorial on setting up jaxb marshaller in springws. I would rather use xml mappings than annotations, although for now I am getting the error message.
EDIT: ChangePersonType
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "ChangePersonType", propOrder = {
"applicationArea",
"dataArea"
})
public class ChangePersonType {
#XmlElement(name = "ApplicationArea", namespace = "http://mycompany/coolservice/specs", required = true)
protected TransApplicationAreaType applicationArea;
#XmlElement(name = "DataArea", namespace = "http://mycompany/coolservice/specs", required = true)
protected DataArea dataArea;
#XmlAttribute(required = true)
#XmlJavaTypeAdapter(NormalizedStringAdapter.class)
protected String releaseID;
#XmlAttribute
#XmlJavaTypeAdapter(NormalizedStringAdapter.class)
protected String versionID;
--The rest are getters and setters.
I solved it. The parameter of the end point class and return variable had to be wrapped in JAXBElement, like JAXBElement.
The reason is
The classes generated by JAXB2 from
your schema come in two flavors: those
that have a #XmlRootElement
annotation, which can be used directly
as either parameter or response, and
those who haven't. Those classes which
haven't got this annotation need to be
wrapped in a JAXBElement.
Besides the generated classes from
your schema, JAXB2 also generates an
ObjectFactory class, which clarifies
the use of JAXBElement. There are some
factory methods is there, which
illustrate how you can use the various
schema types.
Arjen Poutsma
h ttp://forum.springsource.org/showthread.php?t=49817

Categories

Resources