0

So, this is what I want to do. Some like so I was already able to find in openerp.

I think, I could simply declare a bean, which were a JPA persistent entity as well, for example:

Example.java:

package my.project;

@Configurable
@Entity
public class Example {
    String a;
    String b;
...
}

Of course, this class already contains the JPA persistence injections, thus it can be handled from java code as a normal JPA-backed persistent class. My goal is to declare some of its instances from the Spring applicationContext.xml:

applicationContext.xml:

<bean class="my.project.Example">
    <property name="a" value="test_a"/>
    <property name="b" value="test_b"/>
</bean>

What I think it were really interesting:

  1. The persist() method of the newly created instance had to be called from the Spring initialization, but only if it doesn't exist in the DB already.
  2. If it exists, the instance should be loaded from the persistent JPA storage.

Is it somehow possible? Is there even example code for the task? :-)

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
peterh
  • 11,875
  • 18
  • 85
  • 108
  • Do you want to do it exactly that way or a solution involving a bean with a `PostConstruct` that would load/persist the entity using an injected `EntityManager`? You could then inject that bean into other classes, and access the entity using getter/whatever (but beware of how entity is attached to its entity manager!). – NoDataFound Sep 17 '14 at 15:54
  • @NoDataFound I want to declare persistent JPA entities in applicationContext.xml which were found/created/loaded by the spring initialization automatically. – peterh Sep 17 '14 at 15:57
  • @AndreiI I don't understand you. I didn't want a conceptional change. I only want to declare persistent objects in context.xml -s. – peterh Sep 24 '14 at 12:56
  • Maybe create a factory bean for this, that will check the db if entity exists and return it if so, or create a new one if nothing found? – Nadir Sep 24 '14 at 14:11
  • @Nadir It seems a very good idea for me. I think, some like a general solution were the best, if this factory bean could be implemented even _independently_ from the actual persistent objects. Even as a template, or some other. Do you know some like this? – peterh Sep 24 '14 at 15:41
  • Not sure to understand the question, but maybe this answers your question http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/FactoryBean.html With this approach you can create beans of whatever type you need, and only when you need them. – Nadir Sep 25 '14 at 05:30
  • @PeterHorvath At first I didn't understand what you wanted. Now I do, but I still do not understand your real problem. What exactly are you trying to solve? Why not inserting manually in DB on deploying? – V G Sep 29 '14 at 09:00
  • @AndreiI Because I feel it much better if these persistent objects are declared in the spring context xmls. It were an automatized, generic, xml-based solution for exactly this "insert if needed on deploy" task. OpenERP has a very similar mechanism for that, I wonder why the spring/java world doesn't know this now. I admit your intent to help, but please think about: if I am looking for an answer, maybe a "Why do you want it" question can't make me really happy. – peterh Sep 29 '14 at 10:20

2 Answers2

1

Did you thought about using factory method instead? You could insert Entity manager inside and call find/save inside. Factory method would then returned your bean instance either from database or create a fresh one and persist it. For me this would be better solution, easy to mock in tests also.

Beri
  • 11,470
  • 4
  • 35
  • 57
1

No, there is no existent generic solution for that. What you can do, is simply execute some code from a Bean on deployment, as it is described here. If you want to make it more generic, so that you read the property values from an XML file, you are free to parse the XML data in the Bean and do your generic logic.

Community
  • 1
  • 1
V G
  • 18,822
  • 6
  • 51
  • 89