JPA -#Version for super class - jpa-2.0

I got super class AbstractEntity and all my entities classes (for example A , B ,C) inherited from it.
If I add:
#Version
#Column(name = "optlock", columnDefinition = "integer DEFAULT 0", nullable = false)
private long version = 0L;
in my AbstractEntity will this also work for classes A, B and C? Or maybe I need to add this annotation in each child class?

It will work well for subclasses. It is a common pattern to put create an abstract entity class with id and version fields and extend it.

Related

How to get an Ecore feature disply name without an object instance?

I'd like to create a GUI table to display a given list of features of an EObject sub-class. To do this I have to get the display names of the features for the column header.
How do I get the feature display names in the best way?
One solution that seems a bit like a hack:
If I have an instance of the class then I can use the adaptor factory to get a IItemPropertySource that can do this:
SomeEntity e = ...
String displayName = adaptorFactory.adapt(e, IItemPropertySource.class)
.getPropertyDescriptor(null, feature).getDisplayName(null));
But when the table is empty there is no SomeEntity object handy to use to get the IItemPropertySource.
I can create a dummy object using the EFactory in this way:
EClass containingClass = feature.getEContainingClass();
SomeEntity dummy = containingClass.getEPackage().getEFactoryInstance()
.create(containingClass));
... and then use that object the get the IItemPropertySource. But this seem a bit like a hack. Is there no better solution?
If you know the class at compile time, you can create the ItemProviderAdapter yourself:
MyClassItemProvider provider = new MyClassItemProvider(adaptorFactory);
String name = provider.getPropertyDescriptor(null, property).getDisplayName(null);
If you do not know the class at compile time, but only have an EClass instance at runtime, things are more complicated, because the necessary methods are protected. You have to "make" them public first.
I would add respective methods to the generated MyPackageSwitch and MyPackageAdapterFactory classes (in myPackage.util).
In MyPackageAdapterFactory:
/**
* #generated NOT
*/
public MyPackageSwitch<Adapter> getModelSwitch() {
return modelSwitch;
}
In MyPackageSwitch:
/**
* generated NOT
*/
public T doPublicSwitch(EClass theEClass, EObject theEObject) {
return doSwitch(theEClass, theEObject);
}
Now you can create an ItemProviderAdapter for an EClass theEClass like this:
provider = (ItemProviderAdapter) adapterFactory.getModelSwitch()
.doPublicSwitch(theEClass, null);
EMF was obviously not made for this. Keep in mind that this all is only working if you do not have any custom provider implementations that uses the EObject values.

oneToMany copy between different entityManagers

I have a utility to copy entities between two different databases using two entity managers.
Query q = em1.createQuery("SELECT o FROM Holder o WHERE o.id=1");
Holder holder = (List<Holder>) q.getSingleResult();
em1.clear();
em2.getTransaction().begin();
em2.merge(holder);
em2.getTransaction().commit();
All works fine except oneToMany relations:
#Entity
public class Holder{
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name = "HOLDER_ID")
private Set<Piece> pieces;
}
#Entity
public class Piece{
//No mapped by to holder
}
The result of the operation is that holder is persisted ok and pieces are persisted as well BUT HOLDER_ID is null.
If I explicit a mapped by holder in Piece the joincolumn is copied but I can't change the model to be bidirectional.
Any ideas of what can be wrong? Detaching and merging in the same entityManager works fine too.
UPDATE: The sql generated does not contains HOLDER_ID update so 'it fails' too in the same entityManager.
(I'm using Hibernate as JPA provider).
Ok, I found out that the sql generated if the tables are empty is
1/ Insert for Holder
2/ Insert for each Piece
3/ Update to setup Holder_id in each Piece
But if the tables contains Holder but not pieces
1/ Insert for each Piece
That's the reason pieces losses reference to Holder.

Spring 3 MVC Web services: many parameters with same name

I am running into the next problem. I have declared a method in the controller like the next one, to be used as a web service:
#RequestMapping(value = "/" + "prueba" , method = RequestMethod.GET)
public void prueba(ExampleBean pExample1, ExamlpleBean pExample2) {
// Wonderful code here
}
And the class ExampleBean is just, well, a Bean:
public class ExampleBean implements Serializable {
private String id;
private String whatever;
// getters, setters, and more.
}
If the interface were something like that:
#RequestMapping(value = "/" + "prueba" , method = RequestMethod.GET)
public void prueba(ExampleBean pExample1) {
// Wonderful code here
}
Each time I would like to call that web service, I would call the URL in the next way:
http://myWebProject/prueba?id=1&whatever=hola
But... How can I do when I have to give values to both params from the same class? I mean, I can not repeat parameters, so I dont know how to differ between the id from pExample1, and the id from pExample2 when writing the URL.
I mean, also with two parameters from different classes, but with an attribute with the same name. For example, if the second parameter is from the class DifferentExampleBean, which has also an "id" parameter.
Thanks a lot!
PS: I am using StringHttpMessageConverter.
What you would do is to create a parent class which would hold particular field you're interested in then both ExampleBean and ExampleBean1 would extend this parent class and you'd have only one type to be sent in prueba(ParentClass instance1, ParentClass instance2).
Where instance1 would be instance of ExampleBean and instance2 would be instance of ExampleBean2

Implementing a three-way join relationship in JPA 2.0

I am trying to implement a three-way join relationship in JPA 2.0 (using annotations).
My domain is as follows:
I had a look at the #JoinTable annotation and I am not sure how to use it in order to implement the relationship.
Can anyone please provide clues or code samples?
If I understand your question well, you actually have another Entity, let's call it AdvertisementAssignment. Then, this entity should have OneToOne association with each of your 3-way counterparts.
#Entity
#Table(name = "ADV_ASSIGNMENTS")
public class AdvertisementAssignment {
private Advertisement advertisement;
private TimeSlot timeSlot;
private Day day;
// other properties definition (e.g. id, assigner etc.)
// define constructor
#OneToOne(cascade = CascadeType.ALL)
public Advertisement getAdvertisement() {
return this.advertisement;
}
public void setAdvertisement(Advertisement advertisement) {
this.advertisement = advertisement;
}
// same for 'timeSlot' and 'day' properties
}

MSTest, Accessors, Inheritance, and Private Members

I'm trying to write some tests for an MVC application we're developing. We have a BaseController class that contains the following:
public class BaseController : Controller
{
protected string UserRole { get; private set; }
We then have a controller that inherits from the BaseController:
public class CustomFieldController : BaseController
I've generated private accessors for both classes (just regenerated them a few minutes ago). In one of my unit tests for CustomFieldController I want to set the UserRole, so I've got the following code:
CustomFieldController controller = new CustomFieldController();
CustomFieldController_Accessor accessor = new CustomFieldController_Accessor(
new PrivateObject( controller, new PrivateType( typeof( BaseController ) ) ) );
accessor.UserRole = "OTHER";
Every time I try to run this test it throws an exception on the last line stating:
The member specified (CustomFieldEdit) could not be found. You might need to regenerate your private accessor, or the member may be private and defined on a base class. If the latter is true, you need to pass the type that defines the member into PrivateObject's constructor.
As far as I can tell, I've done what it says. Not only have I recently regenerated the private accessor, but I am passing the type that defines the member into PrivateObject's constructor.
Any thoughts as to what I'm missing here? I know I can make it work by taking the "private" off the property setter, but I'd rather not do that if I can avoid it (don't want subclass implementers thinking they can inject a value into that property).
CustomFieldController controller = new CustomFieldController();
var po = new PrivateObject( controller, new PrivateType( typeof( BaseController ) ) );
CustomFieldController_Accessor accessor = new CustomFieldController_Accessor( po );
po.SetFieldOrProperty("UserRole","OTHER");