Convert Immutable java object to immutable object (Immutable or Builder) - dozer

I want to convert one immutable object to another immutable objects. I googled and found that Dozzer and Orika are mostly used for object mappings.
I tried using Dozzer but seems that it doesn't work well with objects created using builder pattern.
Example:
//Source Object
public class EmployeeDTO{
private String id;
private String name;
private void setName(String name){this.name=name;}
private void setId(String id){this.id=id;}
public static class Builder{
public String id;
public String name;
public Builder setName(String name){this.name=name;}
public Builder setId(String id){this.id=id;}
public EmployeeDTO build(){
EmployeeDTO employeeDTO = new EmployeeDTO();
employeeDTO.setName(this.name);
employeeDTO.setId(this.id);
return employeeDTO;
}
}
}
//Target Object
public class Employee{
private String id;
private String name;
private void setName(String name){this.name=name;}
private void setId(String id){this.id=id;}
public static class Builder{
public String id;
public String name;
public Builder setName(String name){this.name=name;}
public Builder setId(String id){this.id=id;}
public Employee build(){
Employee employee = new Employee();
employee.setName(this.name);
employee.setId(this.id);
return employee;
}
}
}
I want to convert these two structures interchangeably. I have little idea that I can create custommapper to perform this task. Please help me if it is possible with Dozzer/Orika in easy way.

public class SourceObjectClass implements T{
private final String name;
private final int id;
public sourceObjectClass(final String name, final int id){
this.name = name;
this.id = id;
}
public String getName(){
return name;
}
public String getId(){
return id;
}
}
//Destination Object
public class DestinationObjectClass implements T{
private final String name;
private final int id;
public sourceObjectClass(final String name, final int id){
this.name = name;
this.id = id;
}
public String getName(){
return name;
}
public String getId(){
return id;
}
}
//Converter
//Converter
public class MyConverter{
public static T convert(SourceObjectClass sourceObject, Class<T> destinationClass){
Class definition = Class.forName(destinationClass);
return definition.newInstance(sourceObject.getName(),sourceObject.getId());
}
}
public class MyTestClass{
public static void main(String args[]){
//Create an immutable object
SourceClasssObject sourceObject = new SourceClassObject("Guest",100);
//convert the above object into another immutable Object
final DestinationObjectClass destination = MyConverter.convert(sourceObject,DestinationObjectClass.class);
}
}

Related

dropdownchoice with choicerenderer, how to get values

i have problem trying to get some values. this is my situation (wicket)
i have a dropdownchoice study_template, i don't have problem populating the DDC, the problem is when i try to get some value (id or name). this is the code
ChoiceRenderer choiceRenderer = new ChoiceRenderer("name", "id");
study_template = new DropDownChoice( "study_template",new PropertyModel( this, "selectedTemplate" ),template_names , choiceRenderer);
template_names is a list< SelectOption > with the values obtain from a DB. this works OK.
this is the class that i use to populate the DDC
public class SelectOption implements java.io.Serializable {
private long id;
private String name;
public SelectOption(long id, String name ) {
this.id = id; this.name=name;
}
public long getId()
{return id; }
public String getName()
{return name; }
}
normally i can get the value with study_template.getModelObject(), but in this case it doesn't work, i don't have any ideas about to obtain the id and the name , i know that i need GETID() and GETNAME(), but i don't know how to use it, any help will be appreciated
You could use something as below:
public class SpikePage extends WebPage {
class Person {
String id;
String name;
public Person(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public SpikePage() {
Person employee = new Person("E001", "ABC");
Person manager = new Person("E002", "XYZ");
List personList = new ArrayList(2);
personList.add(employee);
personList.add(manager);
Form form = new Form("form");
final DropDownChoice personDropDownChoice = new DropDownChoice("person", new ArrayList(personList), new IChoiceRenderer() {
#Override
public Object getDisplayValue(Person object) {
return object.getId().concat("-").concat(object.getName());
}
#Override
public String getIdValue(Person object, int index) {
return object.getId();
}
});
personDropDownChoice.setOutputMarkupId(true);
personDropDownChoice.setNullValid(false);
form.add(personDropDownChoice);
final Label label = new Label("name");
label.setOutputMarkupId(true);
form.add(label);
personDropDownChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
Person selectedPerson = personDropDownChoice.getModelObject();
label.setDefaultModelObject("Hi ".concat(selectedPerson.getName()));
target.add(label);
}
});
add(form);
}
}
Thanks for your answer, i already get it work
i use this
private SelectOption selectedTemplate;
and then
selectedTemplate.getId();

Bidirectional one to one mapping in GAE using JDO?

How can I implement a bidirectional one-to-one mapping using Google Application Engine (GAE) using Java Data Objects (JDO)?
I have a User class which holds contactInfo object and a ContactInfo class that holds a user object
#PersistenceCapable(identityType ="APPLICATION", detachable = "true")
public class User{
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
private String name;
#Persistent(dependent = "true")
private ContactInfo child;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ContactInfo getChild() {
return child;
}
public void setChild(ContactInfo child) {
this.child = child;
}
}
#PersistenceCapable(identityType ="APPLICATION", detachable = "true")
public class ContactInfo {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key id;
#Persistent(mappedBy = "child")
private User parent;
private String contactDetail;
public Key getId() {
return id;
}
public void setId(Key id) {
this.id = id;
}
public String getContactDetail() {
return contactDetail;
}
public void setContactDetail(String contactDetail) {
this.contactDetail = contactDetail;
}
}
Following error i am getting while testing API from API explorer
com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.demo.jdo.ContactInfo[\"user\"]->com.demo.jdo.User[\"contactInfo\"]->com.demo.jdo.ContactInfo[\"user\"]-
Standard JDO 1-1 bidir is simply found from http://www.datanucleus.org/products/accessplatform_3_1/jdo/orm/one_to_one.html#bi
GAE ought to be no different in this respect; last time I used it (maybe 3 yrs ago) they had some tests, think those under here http://code.google.com/p/datanucleus-appengine/source/browse/#svn%2Ftrunk%2Ftests%2Fcom%2Fgoogle%2Fappengine%2Fdatanucleus
Your question provides no definition of what you have tried in terms of annotations
Got the Solution, problem was with wrong use of mappedBy & presence of getter and setter of parent object in child.
#Persistent(mappedBy = "") annotation should only be at non-owner side
In on-owner/ child side there should not be any getter/setter present for owner/parent object.
Working code:
User.java
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
#PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class User {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
private String name;
#Persistent(dependent = "true")
private ContactInfo contactInfo;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ContactInfo getContactInfo() {
return contactInfo;
}
public void setContactInfo(ContactInfo contactInfo) {
this.contactInfo = contactInfo;
}
}
ContactInfo.java
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.appengine.api.datastore.Key;
#PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class ContactInfo {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key id;
#Persistent(mappedBy = "contactInfo")
/*
* Important: Do not create getter and setters for this object else
* bidirectional mapping gives error
*/
private User user;
private String contactDetail;
public Key getId() {
return id;
}
public void setId(Key id) {
this.id = id;
}
public String getContactDetail() {
return contactDetail;
}
public void setContactDetail(String contactDetail) {
this.contactDetail = contactDetail;
}
}

Nested Embedding in Kundera

I have 2 Embeddable objects and 1 Entity object. I want to use first Embeddable object inside another. Currently its not working for me. Below is the code
Class 1
#Embeddable
public class Object1{
public Object1{
}
#Column(name = "name")
String name;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
Class 2
#Embeddable
public class Object2{
public Object2{
}
#Column(name = "name")
String name;
#Embedded
#Column(name = "object1")
Object1 object1;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Object1 getObject1(){
return object1;
}
public void setObject1(Object1 object1){
this.object1 = object1;
}
}
Entity Class
#Entity
#Table(name = "xxx", schema = "yyy#zzz")
public void EC{
public EC(){
}
#Embedded
#Column(name = "object2")
Object2 object2;
public Object2 getObject2(){
return object2;
}
public void setObject2(Object2 object2){
this.object2 = object2;
}
}
When I run this program, only name of Object2 is getting saved but not the embedded Object1
Is this structure is possible in Kundera? Or what am I doing wrong?
allowed only for Cassandra's composite partition key
-Vivek

"DataNucleus Enhancer completed with an error"

Am trying to create a model class to store an Entity in Google App Engine using Eclipse.But when i save my work i get the error message:
org.datanucleus.metadata.MetaDataManager initialiseFileMetaDataForUse.
SEVERE: Class "com.packagename.classname" has been specified with an object-id class javax.jdo.identity.StringIdentity yet no fields have been identified as primary key fields. Please notate using the "primary-key" tag against the fields that should be considered part of the primary key.
If my understanding of JPA is correct, i do not need a primary-key for an entity since i already have a #Id tag.Here is my class.
#Entity
public class MyCLassName {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private static String userName;
private static String location;
private static Date dateOfBirth;
private static int age;
private static String gender;
private static String teamName;
private static int weight;
//Constructor with arguments
public MyClassName(String userName, String location, String gender, int age, Date DOB, int weight) {
MyClassName.userName = userName;
MyClassName.location = location;
MyClassName.gender = gender;
MyClassName.age=age;
MyClassName = DOB;
MyClassName.weight = weight;
}
//setter methods
public static void setUserName(String userName) {
MyClassName.userName = userName;
}
public static void setLocation(String location) {
MyClassName.location = location;
}
public static void setGender(String gender) {
MyClassName.gender = gender;
}
public static void setAge(int age) {
MyClassName.age = age;
}
public static void setDateOfBirth(Date dateOfBirth) {
MyClassName.dateOfBirth = dateOfBirth;
}
public static void setWeight(int weight) {
MyClassName.weight = weight;
}
//getter methods
public static String getUserName() {
return userName;
}
public static int getWeight() {
return weight;
}
public static String getLocation() {
return location;
}
public static String getGender() {
return gender;
}
public static String getTeamName() {
return teamName;
}
public static Date getDateOfBirth() {
return dateOfBirth;
}
public static int getAge() {
return age;
}
}
What exactly am i doing wrong here?
Your understanding of JPA is partially correct. You do not need to assign the primary key because you have used the #Id and #GeneratedValue annotation. The JPA implementation will automatically generate the primary key value as a long integer. However it still needs a field in which to store this ID value. It is trying to do that in userName. See Java Tutorial ID Generation Type: IDENTITY for example.

Copy Entity ID at persist time

I want to copy the entity's UUID, generated at run time to another field.
The entity id is generated via the code described bellow:
package eclipselink.example;
public class UUIDSequence extends Sequence implements SessionCustomizer {
public UUIDSequence() {
super();
}
public UUIDSequence(String name) {
super(name);
}
#Override
public Object getGeneratedValue(Accessor accessor,
AbstractSession writeSession, String seqName) {
return UUID.randomUUID().toString().toUpperCase();
}
...
public void customize(Session session) throws Exception {
UUIDSequence sequence = new UUIDSequence("system-uuid");
session.getLogin().addSequence(sequence);
}
}
Persitence.xml:
property name="eclipselink.session.customizer" value="eclipselink.example.UUIDSequence"
The entity:
public abstract class MyEntity{
private String id;
private String idCopy;
#Id
#Basic(optional = false)
#GeneratedValue(generator="system-uuid")
#XmlElement(name = "ID")
public String getId() {
return id;
}
}
How can I instruct JPA (Eclipse-link) to copy the UUID generated at runtime to idCopy field as well?
I'm not 100% sure this will work (I don't know if EclipseLink calls the setter or assigns the field directly), but give this a try:
public abstract class MyEntity{
private String id;
private String idCopy;
#Id
#Basic(optional = false)
#GeneratedValue(generator="system-uuid")
#XmlElement(name = "ID")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
this.idCopy = id;
// or
// this.setIdCopy(id);
}
}