I have a lot of trouble with the following problem.
I have an Entity "Home" which I use at two different locations within my code.
The problem is, that it is necessary to map this entity to different tables depending
on the class they were used in.
If we assume I would have the two classes Class1 and Class2. Both classes have the
an attribute of the "Home" type. Now I want that of attribute within class1 is mapped to the Table "CLASS1_HOME" and the attribute of class2 is mapped to the table "CLASS2_HOME".
I know that this is possible by e.g., using "MappedSuperClass" for the class "Home" and create a further class "Second_Home" which inherits all attributes from class "home". But my Question is if there is a possibilty to this without inheritance, because the attributes of home will not change and I think the "inheritance" solution is a kind of "dirty". Moreover, I want to this only by using annotations.
Is there a way to do it as I described it ?
If you don't want to use inheritance, and if you are saving HOME the same way in both CLASS1_HOME and CLASS2_HOME table, I do suggest something like this:
public class Home{
boolean Class1;
}
in this case when you persist it you can simply figure out if this is for Class1 or not. And if you want, based on the flag persisted in your database, you can create two views of CLASS1_Home and CLASS2_Home
Related
This is a confusing question to word. Let's say I have a class, Fruit, which has fields Color, Name, etc. (simplified example) If I have a Color (let's say it's a whole class not a primitive) is there a term for the Color's association to Fruit as the "color" field? The application for this use is more of a Java wrapper around a JSON object. Given an instance of a property within a JSON hierarchy I want to add a custom metadata field that contains the name of the field within its parent that it resides within. e.g. I want the Color class to have a field that is populated with the name of the field name it is within in the containing Fruit class.
Is there a term for this association? The child's parent's field name that represents this child? I'm trying to name a variable and "parentFieldNameForThisChild" is stupid and long and I feel like there must be a formal name for this.
To clarify, the parent object is not what I am after. If the Fruit class looks like this:
class Fruit {
Color fruitColor;
String fruitName;
}
If I am a Color object I want to know that I am contained within the "fruitColor" property of my parent, the Fruit class. This is the association I am looking for a term for. Again, this is a simplified example, and will be applied to a JSON wrapper where the property name is not known explicitly in the code and must be retrieved from the JSON structure.
The jquery association is simple parent. http://api.jquery.com/parent/
Because of its hirarchy the access is easy and an additional Field is redundant. Theres is always only one patent, so a type Definition is not necessary. My vote is "parent".
What you are describing is object composition & aggregation relationship.
In your example, a Fruit has a Color, but a Color can exist on its own (or can it ?) - it really depends on your design. Anyway the relationship is either composition or aggregation.
You may want to have a generic name for the relationship, e.g. association-name and the type of the association in another field, e.g. association-type and why not association-cardinality.
This SO thread or this one can help to understand the different relationships between objects.
You can find some naming ideas in a specification language like Unified Modeling Language, and here in the UML glossary.
In a Wicket/Spring/Hibernate project I inherited I find the following pattern:
For each Entity there exists an abstract class called EntityHome generated by Hibernate Tools which provides methods for finding, persisting, merging, and removing Entity. In another package there are classes called EntityDao for each EntityHome which in most cases simply extend EntityHome without adding any additional functionality.
Normally I would create a single generic DAO for handling persist, find, merge, and remove generically and have all DAOs extend this one.
The closest thing to some kind of documentation for Home Objects I found is http://docs.jboss.org/seam/1.1GA/reference/en/html/framework.html#d0e6756 and what I read there pretty much matches what a DAO should do.
So my question is: what is the difference between a Home Object and a DAO? Is there any at all?
I have some repetitive code that is used by a few of my actions in Struts 2.
Needless to say I want to have this code only exist in one place, so I will collect it up into a method and put it ... where?
What is the best practice? Do I create a helper class for each type of helper method? One big helper method? One big static class? A few static classes?
I'm using MVC.
I've read other answers on stackoverflow and none seem to quite answer my question.
Many thanks for your help.
EDIT update with examples, as requested:
For instance:
I have a couple of lines of code that adds an arraylist to the session, which stores when a certain object has been 'rated' (for that session). Its called in a few actions across the application.
Also, I have a view component that is included on multiple JSP pages, and needs to be loaded with some data from the model. I would need to copy/paste the code into each action (obviously want to avoid this).
Hope that clarifies. Please let me know if it does not.
My general rule is that if the methods are computational, in other words if they just perform a function (like math), I will create a class with static methods and use it all over the place.
I have a utility package that I include in a lot of my projects as a jar with string manipulation and validation functions for instance because they rarely change.
String validation is a good example. I have a Validation class that I use a lot to check for null or empty strings and return a boolean. I just call it from my action classes like:
if(Validation.string(value)){
// do magic - huzzah
}
I try to group utility methods into classes especially if I use then all over the place. It tends to save me from re-typing, searching through classes for a good idea I had whenever, and provides a single instance of the code in case I need to update, modify, overload, or override.
For view components included in multiple JSP pages and objects saved to session, I've created a base action class and sub-action classes to avoid repetitive code.
public class BaseAction extends ActionSupport implements SessionAware {
protected Map session;
protected ResourceBundle rb;
// common getters for shared view components and common setters
...
}
--
public class SubAction extends BaseAction {
...
I generated mapping files and POJOs in Netbeans instead of writing them myself. Is it possible to use a derived class in a place of an inherited class? An example would be something like this:
Person.hbm.xml - mapping file
Person.java - generated class (strategy class per table)
PersonExtended - class that extends Person.java
So when I create a new object:
PersonExtended personextended = new PersonExtended(<parameters>);
Would I be able to call methods like:
session.save(personextended) or session.delete(personextended)
?
Is this scenario sensible or should I add any code that I need to in a generated class? Thanks in advance for help or suggestions.
-------Edit--------
In my database I don't have the typical structure that would be possible to be mapped as an inheritance. I merely want to keep the additional methods separate from the main java class for an entity.
Best Regards,
sass.
you will have to tell hibernate how your extended classes should be mapped via a the hbm.xml file. Depending on the strategy Hibernate should use for polymorphism you might have to assign a descriminator value. there are 3 different strategies when using subclasses known as "table per class" "table per concrete class" and "table per subclass".
You can define subclasses in the hbm-xml file by using the <subclass> or <joined-subclass> elements
if you correctly defined your hbm.xml file you can then use session.save(new PersonExtended()) or sth.
you can read up on this here:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html
hope that helped..
I'm using JPA with joined inheritance and a database structure that looks like:
ACTION
---------
ACTION_ID
ACTION_MAPPING_ID
ACTION_TYPE
DELIVERY_CHANNEL_ACTION
--------------------------
ACTION_ID
CHANNEL_ID
OVERRIDE_ADDRESS_ACTION
--------------------------
ACTION_ID
(various fields specific to this action type)
So, in plain English, I have multiple different types of action, all share an ACTION_MAPPING, which is referenced from the 'parent' ACTION table. DELIVERY_CHANNEL_ACTION and OVERRIDE_ADDRESS_ACTION both have extra, supplementary data of their own, and are mapped to ACTION with a FK.
Real-world, I also have a 'suppress' action, but this doesn't have any supplementary data of its own, so it doesn't have a corresponding table - all it needs is an ACTION_MAPPING, which is stored in the ACTION table.
I'm creating a new project from scratch, so am pretty flexible in what I can do, but obviously would like to get it right from the outset!
My current implementation, which works, has three entities loosely defined as follows:
#Entity
#Table(name="ACTION")
#Inheritance(strategy=InheritanceType.JOINED)
#DiscriminatorValue("SUPPRESS")
public class Action
#Entity
#Table(name="DELIVERY_CHANNEL_ACTION")
#DiscriminatorValue("DELIVERY_CHANNEL")
public class DeliveryChannelAction extends Action
#Entity
#Table(name="OVERRIDE_ADDRESS_ACTION")
#DiscriminatorValue("OVERRIDE_ADDRESS")
public class OverrideAddressAction extends Action
That is - I have a concrete base class, Action, with a Joined inheritance strategy. DeliveryChannelAction and OverrideAddressAction both extend Action.
What feels wrong here though, is that my Action class is the base class for these two actions, but also forms the concrete implementation for the suppress action.
For the time being this works, but at some point more actions are likely to be added, and there's every chance that some of them will, like SUPPRESS, have no supplementary data, which will start to get difficult!
So... what I would like to do, in the object model world, is to have Action be abstract, and create a SuppressAction class, which is empty apart from having a #DiscriminatorValue("SUPPRESS").
I've tried doing exactly what is described above, so, changing Action to:
#Entity
#Table(name="ACTION")
#Inheritance(strategy=InheritanceType.JOINED)
public abstract class Action
and creating:
#DiscriminatorValue("SUPPRESS")
public class SuppressAction extends Action
but no luck - it seems to work fine for DeliveryChannelAction and OverrideAddressAction, but when I try to create a SuppressAction and persist it, I get:
java.lang.IllegalArgumentException: Object: com.mypackage.SuppressAction[actionId=null] is not a known entity type.
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4147)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:368)
at com.mypackage.test.util.EntityTestUtil.createSuppressAction(EntityTestUtil.java:672)
at com.mypackage.entities.ActionTest.testCRUDAction(ActionTest.java:27)
which I assume is down to the fact that SuppressAction isn't registered as an entity, but I don't know how I can do that, given that it doesn't have an associated table.
Any pointers, either complete answers or hints for things to Google (I'm out of ideas!), most welcome.
You can, with InheritanceType.SINGLE_TABLE
There are ways to have multiple inheritance types for different branches. See this question.
I think I found a solution that should work for me, my entities now look like:
#Entity
#Table(name="ACTION")
#Inheritance(strategy=InheritanceType.JOINED)
#DiscriminatorValue("SUPPRESS")
public abstract class Action
#Entity
#Table(name="DELIVERY_CHANNEL_ACTION")
#DiscriminatorValue("DELIVERY_CHANNEL")
public class DeliveryChannelAction extends Action
#Entity
#Table(name="OVERRIDE_ADDRESS_ACTION")
#DiscriminatorValue("OVERRIDE_ADDRESS")
public class OverrideAddressAction extends Action
#Entity
#Table(name="ACTION")
#DiscriminatorValue("SUPPRESS")
public class SuppressAction extends Action
So, basically all I was missing was referencing the 'parent' table in my SuppressAction class, which I had foolishly assumed wouldn't work. All my tests are passing with this, and it seems to be working; it seems like it will be happy if I need to add other Actions in the future, a copy of SuppressAction, with a different #DiscriminatorValue works as expected, so I think I'm happy.