Java Mapping objects to objects - Moo - java

I have a question about Moo (https://github.com/geoffreywiseman/Moo/) that I haven't been able to solve on my own. I have this class structure:
class Middle{
private int id;
private Upper upper;
private List<Child> children;
private List<Middle> brothers;
}
class Upper{
private int id;
private String name;
private String lastname;
}
class Child{
private int id;
private String name;
}
and I want to translate them to:
class OutputMiddle{
private int id;
#Property(translation="Upper")
private OutputUpper outputUpper;
#CollectionProperty(itemTranslation = Upper.class)
private List<OutputChild> outputChildren;
private List<OutputMiddle> outputBrothers;
}
class OutputUpper{
private int id;
private String outputName;
}
class OutputChild{
private int id;
private String outputName;
}
What I don't know is:
Am I able to translate one attribute from one class to another attribute of another class?
And do the same but to a collection of objects?
And the same but to a collection of objects of the same class?
Why do I need this? Because I'm returning objects of the class "Middle" as JSON (or XML) and I need it to have an structure such as the "OutputMiddle", but I don't want to intervene the JSON after its creation and change the names of the nodes manually.
Thanks!

Try JMapper Framework, it's ease to use and require few configuration

Related

Complex Object as Inner Class inside Hibernate Entity

Now, I'm aware that an Inner class cannot be an Entity in Hibernate.
I'll first show my code, please refer to my question below:
#Entity
#Table(name = "bags")
public class Bags extends AbstractModel {
private String brand;
private String condition;
private String size;
private Extras extras;
#ManyToOne
private Customer customer;
private class Extras {
private boolean box;
private boolean authenticity_card;
private boolean shoulder_strap;
private boolean dustbag;
private boolean pouch;
private boolean padlock_and_key;
private boolean bagcharm;
private boolean nameTag;
private boolean mirror;
}
}
Getters and setters are ommited. My question is:
If I want to have a slightly more complex object such as Extras, in which I represent the absence or not of several accessories, would it be better to create an additional table associated with bags OR is there a way around this?
Please let me know if I was not clear or you require additional information.
#Embeddable annotation is used to declare a class will be embedded by other entities.
#Embeddable
public class Extras {
private boolean box;
private boolean authenticity_card;
private boolean shoulder_strap;
private boolean dustbag;
private boolean pouch;
private boolean padlock_and_key;
private boolean bagcharm;
private boolean nameTag;
private boolean mirror;
}
#Embedded is used to embed a type into another entity.
#Entity
#Table(name = "bags")
public class Bags extends AbstractModel {
private String brand;
private String condition;
private String size;
private Extras extras;
#ManyToOne
private Customer customer;
#Embedded
private Extras extras;
}

How to convert List<Category> to tree like structure using java 8?

I would like to convert List<Menu> to List<MenuTree> with childMenus within the MenuTree object. How can I achieve this using java 8 streams?
public class Menu {
private Long id;
private String name;
private Long parentId;
}
public class MenuTree {
private Long id;
private String name;
private List<MenuTree> childMenus;
}
Create a MenuTree for each Menu. Store them all in a Map<Long, MenuTree> where the key is the ID of the MenuTree.
Then iterate and for each MenuTree, find its parent by ID in the map, and add it to the children of its parent.
Then find all the MenuTree which don't have a parentId and put them in a List. That's your result.

mapping a composite key with hibernate

I'm trying to map one to many relationship using Hibernate :
I have a profil with multiple properties:
Profil.java
public class Profil {
private String idProfil;
private String name;
private Set<Properties> setProperties;
}
Properties.java
public class Properties {
private CompositeId id;
private String length;
private String heigth;
private String width;
}
and a compositeId.java
public class CompositeId {
private String idPropertiy;
private String idProfil;//idProfil is a foreign key
}
idProperty is assigned manually.
idProfil is assigned automatically.
can anyone help me to write the hbm.xml files.
You can use Hibernate Synchronizer Eclipse plugin to generate the *.hbm files and its associated java classes.
HibernateSynchronizer

How to put propper DBFLow annotation

I want to insert doctor object to database, how should I put annotations for properties?
I tried to do it with te code shown below.
But i don't know how to do it on list properties specializations and phoneNumbers.
#Table(databaseName = WMDatabase.NAME)
public class Doctor extends BaseModel{
#Column
#PrimaryKey
#Unique(unique = true)
private String doctorId;
#Column
private FullName fullName;
#Column
private String organizationId;
#Column What shuld i put here?????
private List<Specialization> specializations;
#Column What shuld i put here?????
private Contacts contacts;
}
Below are the classes I use for doctor attributes:
public class Contacts extends BaseModel {
private List<PhoneNumber> phoneNumbers;
private String email;
private String fax;
}
public class Specialization extends BaseModel {
#Column
#PrimaryKey
#Unique(unique = true)
private String doctorId;
#Unique(unique = true)
private String specializationName;
public String getSpecializationName() {
return specializationName;
}
public void setSpecializationName(String specializationName) {
this.specializationName = specializationName;
}
DBFlow is a relational database system (not a mongo-type key/value store) and doesn't support lists as columns, according to the doc here.
List : List columns are not supported and not generally proper for a relational database. However, you can get away with a non-generic List column via a TypeConverter. But again, avoid this if you can.
The documentation on relationships may help you refine the model to suit your needs.

Morphia: can two objects pull from the same collection if one is a subset of the other?

Lets say I have two very basic classes that share most of their information that I want to store in mongo. Like this:
public class Customer{
#id
private int customerID;
private String Name;
private int social;
private long balance;
}
public class CustomerInfo{
#id
private int customerID;
private String Name;
private int social;
private long balance;
private Address address;
private PhoneNumber phoneNumber;
}
If I don't want to have two nearly-identical collections when one should do. Can I create a single collection for CustomerInfo and still populate the Customer class with the records contained inside, since they are a subset of CustomerInfo?
I know I can load customerInfo with only the values I care about, but I would like something that is a little more elegant, and which plugs into my existing format better.
#Entity(value="customers", noClassnameStored=false)
public class Customer {
#Id
private ObjectId id;
private String Name;
private int social;
private long balance;
}
public class CustomerInfo extends Customer {
private Address address;
private PhoneNumber phoneNumber;
}
It should work like that. Since you are storing the class name in each document, you can easily query for the one you want to use; even though Morphia would figure this out on its own based on the attributes.
PS: And unless you know what and why you are doing it, keep your ID an ObjectId.
PPS: You might want to use object data types instead of primitives in case they can be empty — in which case they won't be stored and use up space in in your database.

Categories

Resources