I've set up Spring Roo following some basic guides, and I have a setup where data from my database can be accessed from a web browser using Roo's standard forms (like in this Youtube video). Now I'd like to access that data from Java code so I can "inject" it into a view from other pages on the site. How is this done?
Edit: Here's how I was able to access my data: From a controller (or any class, really), I use the this annotation along with this property definition:
@PersistenceContext
private EntityManager manager;
Then I can access the data with a query like this:
List<Announcement> results = manager.createQuery("from Announcement a where a.id = :id").setParameter("id", new Long(1)).getResultList();
This will give you a List of type Announcement (which is just an entity I created). Of course this query will yield only one result (or zero if the database doesn't have an entry with an id of 1). Thanks Micha for this solution.
You can use the @PersistenceContext
annotation to get a JPA EntityManager
instance in your application. Using the EntityManager
you can query the database (like showed here). Since you are using Roo the entityManagerFactory
bean and transaction support should already be included in your bean configuration file.
You can also use Spring data repositories to access your data.
Maybe this video can help you.