Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use Guice Persist (Guice 3.0) with Wicket 1.5?
    primarykey
    data
    text
    <p>I want to find out how to use Guice Persist (Guice 3.0) with Wicket 1.5.</p> <p>I have not been able to find any 'hello world' type examples explaining how to do this, if you can link/provide such an example that would be great, and happily accepted as an answer.</p> <p>In the meantime I'll be trying to create a 'hello world' type example myself, posting the code here as I progress. Help with making my code function properly will also be accepted as an answer.</p> <hr> <p>I have set up a simple wicket project, very similar to the 'hello world' <a href="http://wicketstuff.org/wicket/guice" rel="nofollow">guice example</a> from Wicket Examples, that uses guice for dependency injection. I now want to extend this project to also use JPA and Guice Persist, instead of "Hello World" I want to fetch a User from the database and display its username. I'm trying to achieve this using the instructions from the Guice wiki about <a href="http://code.google.com/p/google-guice/wiki/JPA" rel="nofollow">Guice persist</a>.</p> <p>UPDATE: So, I kinda got it working. In <code>WebApplication.init()</code> I injected a ServetModule like this <code>getComponentInstantiationListeners().add(new GuiceComponentInjector(this, new MyServletModule()));</code> and I also added GuiceFilter at the top of the web.xml file, before wickets filter.</p> <p>Now when I run the application everything works, but I get this warning about using deprecated methods. Will look into this further.</p> <blockquote> <p>WARNING: You are attempting to use a deprecated API (specifically, attempting to @Inject ServletContext inside an eagerly created singleton. While we allow this for backwards compatibility, be warned that this MAY have unexpected behavior if you have more than one injector (with ServletModule) running in the same JVM. Please consult the Guice documentation at <a href="http://code.google.com/p/google-guice/wiki/Servlets" rel="nofollow">http://code.google.com/p/google-guice/wiki/Servlets</a> for more information.</p> </blockquote> <p><em>Directory tree</em></p> <pre><code>. ├── pom.xml └── src └── main ├── java │   └── se │   └── lil │   ├── HomePage.html │   ├── HomePage.java │   ├── MyServletModule.java │   ├── WicketApplication.java │   ├── domain │   │   └── User.java │   └── service │   ├── IService.java │   └── JpaService.java ├── resources │   ├── META-INF │   │   └── persistence.xml │   └── log4j.properties └── webapp └── WEB-INF └── web.xml </code></pre> <p><em>WicketApplication.java</em></p> <pre><code>public class WicketApplication extends WebApplication { @Override protected void init() { super.init(); getComponentInstantiationListeners().add(new GuiceComponentInjector(this, new MyServletModule())); } @Override public Class&lt;? extends Page&gt; getHomePage() { return se.lil.HomePage.class; } } </code></pre> <p><em>HomePage.java</em></p> <pre><code>public class HomePage extends WebPage { private static final long serialVersionUID = -918138816287955837L; @Inject private IService service; private IModel&lt;User&gt; model = new LoadableDetachableModel&lt;User&gt;() { private static final long serialVersionUID = 1913317225318224531L; @Override protected User load() { return service.getUser(); } }; public HomePage() { setDefaultModel(new CompoundPropertyModel&lt;User&gt;(model)); add(new Label("name")); } } </code></pre> <p><em>HomePage.html</em></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org"&gt; &lt;head&gt; &lt;title&gt;Wicket Examples - guice&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;hr /&gt; Value: &lt;b wicket:id="name"&gt;name goes here&lt;/b&gt; &lt;br /&gt; &lt;hr /&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><em>MyServletModule.java</em></p> <pre><code>public class MyServletModule extends ServletModule { protected void configureServlets() { install(new JpaPersistModule("manager1")); filter("/*").through(PersistFilter.class); } } </code></pre> <p><em>IService.java</em></p> <pre><code>@ImplementedBy(JpaService.class) public interface IService { public User getUser(); } </code></pre> <p><em>JpaService.java</em></p> <pre><code>public class JpaService implements IService { @Inject private EntityManager em; @Override @Transactional public User getUser() { Query q = em.createQuery("FROM User"); q.setMaxResults(1); User u = (User) q.getSingleResult(); return u; } } </code></pre> <p><em>User.java</em></p> <pre><code>@Entity @Table (name = "users") public class User { private Long id; private String name; public User() { } public User(String name) { this.name = name; } @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Long getId() { return id; } @SuppressWarnings("unused") private void setId(Long id) { this.id = id; } @Basic @Column(name = "name") public String getName() { return name; } public void setName(String name) { this.name = name; } } </code></pre> <p><em>web.xml</em></p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"&gt; &lt;display-name&gt;wicketwithguice&lt;/display-name&gt; &lt;filter&gt; &lt;filter-name&gt;guiceFilter&lt;/filter-name&gt; &lt;filter-class&gt;com.google.inject.servlet.GuiceFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;guiceFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;filter&gt; &lt;filter-name&gt;wicket.wicketwithguice&lt;/filter-name&gt; &lt;filter-class&gt;org.apache.wicket.protocol.http.WicketFilter&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;applicationClassName&lt;/param-name&gt; &lt;param-value&gt;se.lil.WicketApplication&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;wicket.wicketwithguice&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;/web-app&gt; </code></pre> <p><em>persistence.xml</em></p> <pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"&gt; &lt;persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL"&gt; &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt; &lt;class&gt;se.lil.domain.User&lt;/class&gt; &lt;properties&gt; &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/&gt; &lt;property name="hibernate.hbm2ddl.auto" value="validate"/&gt; &lt;property name="hibernate.show_sql" value="true"/&gt; &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/&gt; &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test"/&gt; &lt;property name="javax.persistence.jdbc.user" value="test"/&gt; &lt;property name="javax.persistence.jdbc.password" value="1234"/&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;/persistence&gt; </code></pre> <p><em>pom.xml</em></p> <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; &lt;groupId&gt;se.lil&lt;/groupId&gt; &lt;artifactId&gt;wicketwithquice&lt;/artifactId&gt; &lt;packaging&gt;war&lt;/packaging&gt; &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt; &lt;!-- TODO project name --&gt; &lt;name&gt;quickstart&lt;/name&gt; &lt;description&gt;&lt;/description&gt; &lt;!-- TODO &lt;organization&gt; &lt;name&gt;company name&lt;/name&gt; &lt;url&gt;company url&lt;/url&gt; &lt;/organization&gt; --&gt; &lt;properties&gt; &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt; &lt;hibernate-core.version&gt;3.6.4.Final&lt;/hibernate-core.version&gt; &lt;mysql-connector-java.version&gt;5.1.16&lt;/mysql-connector-java.version&gt; &lt;slf4j.version&gt;1.6.1&lt;/slf4j.version&gt; &lt;log4j.version&gt;1.6.1&lt;/log4j.version&gt; &lt;guice.version&gt;3.0&lt;/guice.version&gt; &lt;wicket.version&gt;1.5.2&lt;/wicket.version&gt; &lt;/properties&gt; &lt;dependencies&gt; &lt;!--GUICE DEPENDENCIES --&gt; &lt;dependency&gt; &lt;groupId&gt;com.google.inject&lt;/groupId&gt; &lt;artifactId&gt;guice&lt;/artifactId&gt; &lt;version&gt;${guice.version}&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;com.google.inject.extensions&lt;/groupId&gt; &lt;artifactId&gt;guice-servlet&lt;/artifactId&gt; &lt;version&gt;${guice.version}&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;com.google.inject.extensions&lt;/groupId&gt; &lt;artifactId&gt;guice-persist&lt;/artifactId&gt; &lt;version&gt;${guice.version}&lt;/version&gt; &lt;/dependency&gt; &lt;!-- HIBERNATE DEPENDENCIES --&gt; &lt;dependency&gt; &lt;groupId&gt;org.hibernate&lt;/groupId&gt; &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt; &lt;version&gt;${hibernate-core.version}&lt;/version&gt; &lt;/dependency&gt; &lt;!-- MYSQL DEPENDENCIES --&gt; &lt;dependency&gt; &lt;groupId&gt;mysql&lt;/groupId&gt; &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt; &lt;version&gt;${mysql-connector-java.version}&lt;/version&gt; &lt;/dependency&gt; &lt;!-- WICKET DEPENDENCIES --&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.wicket&lt;/groupId&gt; &lt;artifactId&gt;wicket-core&lt;/artifactId&gt; &lt;version&gt;${wicket.version}&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.wicket&lt;/groupId&gt; &lt;artifactId&gt;wicket-guice&lt;/artifactId&gt; &lt;version&gt;${wicket.version}&lt;/version&gt; &lt;/dependency&gt; &lt;!-- LOGGING DEPENDENCIES - LOG4J --&gt; &lt;dependency&gt; &lt;groupId&gt;org.slf4j&lt;/groupId&gt; &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt; &lt;version&gt;${slf4j.version}&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.slf4j&lt;/groupId&gt; &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt; &lt;version&gt;${log4j.version}&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; &lt;artifactId&gt;servlet-api&lt;/artifactId&gt; &lt;version&gt;2.5&lt;/version&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt; &lt;version&gt;2.3.2&lt;/version&gt; &lt;configuration&gt; &lt;source&gt;1.6&lt;/source&gt; &lt;target&gt;1.6&lt;/target&gt; &lt;optimize&gt;true&lt;/optimize&gt; &lt;debug&gt;true&lt;/debug&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-eclipse-plugin&lt;/artifactId&gt; &lt;version&gt;2.8&lt;/version&gt; &lt;configuration&gt; &lt;downloadSources&gt;true&lt;/downloadSources&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt; &lt;repositories&gt; &lt;repository&gt; &lt;id&gt;Apache Nexus&lt;/id&gt; &lt;url&gt;https://repository.apache.org/content/repositories/snapshots/&lt;/url&gt; &lt;releases&gt; &lt;enabled&gt;false&lt;/enabled&gt; &lt;/releases&gt; &lt;snapshots&gt; &lt;enabled&gt;true&lt;/enabled&gt; &lt;/snapshots&gt; &lt;/repository&gt; &lt;/repositories&gt; &lt;/project&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload