Where to fill JPA database with initially available values? [duplicate] - jpa

This question already has an answer here:
How to get transactions to a #PostConstruct CDI bean method
(1 answer)
Closed 5 years ago.
I have a Java EE 7 project with a web frontend and I'd like to present some lazily-initialized initial choice to the user which he_she can then complement. The #PostConstruct method of the EJB seemed like a good place, but it doesn't guarantee that a transaction is present.
I'm looking for the most elegant solution, eventually a place which has been foreseen for this task.
The #PostConstruct method of an #ApplicationScoped managed bean in the web frontend would be an option, but I'd still have to make a bogus call to get it initialized - it's way, but maybe not the most elegant one.

Specify the javax.persistence.sql-load-script-source property in your persistence.xml file.
It points at a SQL script that will preload your database. This can be a resource embedded in your application or an external file URL.

Related

Adding transaction support to embedded jetty/GWT RemoteServiceServlet without Spring?

GWT's servlet implementation has onBefore/onAfterDeserialization which would give me a hook with which to start and stop transactions without doing anything fancy, however those methods don't allow me to properly check for error conditions after the service method got invoked, I just have access to the serialized return value, not directly to any exception that might have been thrown, so deciding whether to roll back or not is not possible that way without rewriting parts the GWT servlet.
I was thinking about using aspectj's compile-time weaving. However, this does not work with Netbeans' compile-on-save feature because the module needs to be recompiled using the aspectj compiler.
How about LTW (load-time-weaving)? Is there any way (or example) to add LTW to the webapp container without using the Spring framework?
I was also thinking about using AOP based on Java dynamic proxies, ie. to put a proxy in front of the servlet. Again, the question arises how to tell the Jetty WebApp container to load the proxy instead of the original servlet.
Or is there any ready-to-use solution out there already?
I think you could overwrite a combination of
public String processCall(RPCRequest rpcRequest) from RemoteServiceServlet and RPC.invokeAndEncodeResponse to do what you want.
Not ideal, as you need to copy/paste a few lines of code, but they really are only a few.
I myself hit the same problems as I needed some customizations, and relevant methods didn't had the access modifier that I needed, so I ended up copy/pasting some portions.
I can't comment on the rest of your question, but I don't expect to find any ready-to-use solutions, as GWT-RPC doesn't seem to have any new fans out there; just people maintaining legacy systems. Therefore, I expect that you either don't find anything or find solutions that are no longer maintained.

Do I need JSF to build Frontend when I have REST service? [duplicate]

This question already has answers here:
How to implement JAX-RS RESTful service in JSF framework
(3 answers)
JSF Controller, Service and DAO
(2 answers)
Closed 5 years ago.
I am learning to build a JavaEE application, and I find that JSF's controller layer very similar to REST service, they all invoke the service layer's methods to reach the same goal. So if I already have a REST service and then want to build the front-end, do I need JSF? Or can I just use HTML/AJAX? To be frankly, the real question I'm now facing is that how to interagte the front-end and back-end together. I'm really confused about it, please help me, thank u so much!
The REST service IS your interaction layer.
Whether it talks to a JSF application, an Angular application, a C# application, Cobol, or whatever is completely irrelevant.
Many REST services never talk to a web frontend at all.

jboss ignores requires_new after restart

I’m using jboss and cmt and have seen strange behaviour when using requires_new on an ejb method that I loop over from another bean to insert some records.
I see that intermittently after restarting the jboss the operations in the method aren’t committed to db after the method is finished.So, I have tried to use TransactionSynchronizationRegistry and found that when the data isn’t committed I actually don’t get a new transaction each time the method is entered. Anyone who has heard of jboss acting this way? I'm using ejb, jboss, jpa, Hibernate, cmt.
So, I've finally solved this problem. I used this great blog post
http://piotrnowicki.com/2011/11/am-i-in-the-same-transaction-am-i-using-the-same-persistencecontext/
to make sure that I actually was in the same transaction after entering the method annotated REQUIRES_NEW. So then I understood that JBoss intermittently ignored the annotation and defaulted to REQUIRED instead. This was because multiple classes implemented the same interface. So I simplified it so that I now have one interface, annotated #Local, and one implementing class where the REQUIRES_NEW annotated method is.
I understand that in this case it was our code that was wrong so it's not really a bug in Jboss way of handling transactions. But it's really strange that there isn't as much as a warning in the logs that there is a race condition. I could restart the Jboss and get a different behavior than before the restart. I think that's strange. I haven't found much about that this could be a problem after googling so I hope that this answer can save some time for someone else.
Edit:
It's not about multiple implementing classes, just about declaring all the methods in the interface annotated #Local, otherwise JBoss might ignore the attributes

JavaEE changing DB schema runtime for SaaS application

i am developing a SaaS application by using JavaEE technologies (JPA, EJB, ..)
for multitenancy, i chose 'Shared Database, Separate Schemas' approach and designed the DB (PostgreSQL) like that
basically what i need to do is changing the default schema for user sessions in the application so that users can get their own data from the right schema
my business logic is implemented with EJB (Container-managed) and the app server is glassfishv3
so in my EJBs i am just injecting the EntityManager like this
#PersistenceContext(unitName="DBNAME")
private EntityManager em;
and leaving the transaction management to the glassfish
i tried to write #PostConstruct callbacks for Stateless EJBs injecting DataSource
but getClientInfo() returns null somehow so i can not even see the default schema. The reason why i injected DataSource was because i thought i have to do some low-level stuff to specify the schema.
i know if i manage the transactions in the application instead of leaving them to the app server, i can change the EntityManager values through EMF easily but i wanna keep the Container-managed infrastructure and just change some values at runtime
is there any way to do this with SessionContext or anything else ?
what is the best way of overcoming this issue ?
thanks in advance
#PostConstruct is by definition the wrong way, because it is only a hint to the bean that it has been constructed and its dependencies have been injected. The bean is now in the pool of EJBs waiting for the first service call. Then it will be connected to the client info of that call. After that call the client info is disconnected again.
I think any kind of standard injection and standard container managed persistence stuff will not work in your case because injection is done exactly once (at EJB creation time) but your case would require injection for each service call/transaction _AND your injection would depend on input data - the client request containing the tenant id.
Also: I hope, you have only up to 6 (or so) tenants due to the maintenance and performance burden of that approach. I have never seen this approach working in the wild.

Accessing data across sessions

I've recently adopted the GWT framework and I've run into trouble.
I'm creating a simple web-app which provides an input textarea and a list where the written articles are listed, a guestbook application if you will.
Now the problem is that I can't figure out how to maintain the list in a servletContext() - a global list. I can store data in a single session, but that wont do any good, since the point is that users have to look at the same list, not an individual one.
With Java servlets I'm used to storing objects in the ServletContext() which is globally available, but for the love of me I can't figure out how to do this with GWT.
Does anyone know how I can achieve this?
Thank alot!
Pretty straightforward -
Write a GWT RPC service as explained here - http://code.google.com/webtoolkit/doc/latest/DevGuideServerCommunication.html
The class that you write is just a normal java servlet. So you can invoke do the following to get the ServletContext
ServletContext servletContext = getServletContext();
Having said that, you are better off storing the data to a database. ServletContext will not persist the data, it will be lost the moment you restart your server.