-
15. Re: Setting up Hibernate
mayerw01 Aug 1, 2016 4:31 AM (in response to royibernthal)I personally prefer this documentation JDBC Driver / DataSource Setup · HAL Management Console
Since it uses MySQL in the example you may also just copy and paste
-
16. Re: Setting up Hibernate
royibernthal Aug 1, 2016 11:40 AM (in response to mayerw01)I installed the MySQL driver as deployment.
Database Name: test
I tried to setup a Non-XA MySQL datasource as follows:
Name: test
JNDI Name: java:/jboss/datasources/test
In detected drivers I had 2 options:
(1) mysql-connector-java-5.1.39-bin.jar_com.mysql.jdbc.Driver_5_1
(2) mysql-connector-java-5.1.39-bin.jar_com.mysql.fabric.jdbc.FabricMySQLDriver_5
I chose (1). What's the difference between them?
Connection URL: jdbc:mysql://localhost/test
I'm not entirely sure what should be in connection url, I just set it to localhost/db_name.
In "Test Connection" it fails to create a JDBC connection:
Unexpected HTTP response: 500
Request
{
"name" => "test",
"enabled" => true,
"jndi-name" => "java:/jboss/datasources/test",
"user-name" => "root",
"pool-name" => "test_Pool",
"use-ccm" => false,
"jta" => false,
"connection-url" => "jdbc:mysql://localhost/test",
"driver-class" => "com.mysql.jdbc.Driver",
"driver-name" => "mysql-connector-java-5.1.39-bin.jar_com.mysql.jdbc.Driver_5_1",
"valid-connection-checker-class-name" => "org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker",
"background-validation" => true,
"exception-sorter-class-name" => "org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter",
"operation" => "add",
"address" => [
("subsystem" => "datasources"),
("data-source" => "test")
]
}
Response
Internal Server Error
{
"outcome" => "failed",
"failure-description" => "WFLYCTL0212: Duplicate resource [
(\"subsystem\" => \"datasources\"),
(\"data-source\" => \"test\")
]",
"rolled-back" => true
}
I tried following the advice in:
java - Duplicate resource Wildfly - Stack Overflow
Both removing the duplicate resource from standlone.xml and renaming the current datasource I'm adding didn't work and result in the exact same error.
I didn't find any < app >.war.failed files to delete.
-
17. Re: Setting up Hibernate
mayerw01 Aug 1, 2016 12:56 PM (in response to royibernthal)I would recommend to use the "Install as Module" option instead. Install as deployment does not work in domain mode anyway.
As mentioned in MySQL :: MySQL Fabric MySQL Fabric provides a framework for managing farms of servers.
Therefore as long as you don't use MySQL server farms the com.mysql.jdbc.Driver should be OK.
You should follow the syntax required by MySQL (MySQL :: MySQL Connector/J 5.1 Developer Guide :: 5.1 Driver/Datasource Class Names, URL Syntax and Configuration Proper…)
The general format for a JDBC URL for connecting to a MySQL server is as follows, with items in square brackets ([ ]) being optional:
jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database] [?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]
I would also not recommend changing anything in standlone.xml. To avoid typing errors you should always use cli or the web console.
-
18. Re: Setting up Hibernate
royibernthal Aug 1, 2016 7:22 PM (in response to mayerw01)I successfully created a datasource, I needed to specify the port.
When I try to test my project, NullPointerException is still thrown.
Here's my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="Hibernate_JPA" transaction-type="JTA"> <jta-data-source>java:/jboss/datasources/test</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <!-- <class>entities.User</class> --> <properties/> </persistence-unit> </persistence>
If I un-comment <class>entities.User</class> I get an error:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'class'. One of '{"http://xmlns.jcp.org/xml/ns/persistence":shared-cache-mode, "http://xmlns.jcp.org/
xml/ns/persistence":validation-mode, "http://xmlns.jcp.org/xml/ns/persistence":properties}' is expected.
-
19. Re: Setting up Hibernate
mayerw01 Aug 2, 2016 5:09 AM (in response to royibernthal)Did you add the @Stateless annotation to your Users class?
Apparently the persistence error is caused by an ordering issue. When using the <class> element it should come before <exclude-unlisted-classes>. But there should not be any need to add this element because of auto detection.
-
20. Re: Setting up Hibernate
royibernthal Aug 2, 2016 9:56 AM (in response to mayerw01)I see, I removed the class element.
Is using the @Stateless annotation a must for this to work properly? I'd like to be able to have instances of this class.
With that said, I tried now adding the @Stateless annotation to my Users class.
I try to inject Users into a field in my REST application using the @EJB annotation, this results in errors.
Page.java
import javax.ejb.EJB; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import entities.Users; @Path("/") public class Page { @EJB(beanName="UsersBean") Users users; @GET @Path("/page") @Produces({ "application/json" }) public String getHiJSON() { return "{\"name\":\"" + users.getUserName() + "\"}"; } }
This is the log I get in the console:
16:40:55,917 INFO [org.jboss.as.server.deployment] (MSC service thread 1-3) WFLYSRV0028: Stopped deployment rest-test.war (runtime-name: rest-test.war) in 3ms
16:40:55,917 INFO [org.jboss.as.server.deployment] (MSC service thread 1-4) WFLYSRV0027: Starting deployment of "rest-test.war" (runtime-name: "rest-test.war")
16:40:55,939 INFO [org.jboss.weld.deployer] (MSC service thread 1-8) WFLYWELD0003: Processing weld deployment rest-test.war
16:40:55,946 INFO [org.jboss.as.ejb3.deployment] (MSC service thread 1-8) WFLYEJB0473: JNDI bindings for session bean named 'Users' in deployment unit 'deployment "rest-test.war"' are as follows:
java:global/rest-test/Users!entities.Users
java:app/rest-test/Users!entities.Users
java:module/Users!entities.Users
java:global/rest-test/Users
java:app/rest-test/Users
java:module/Users
16:40:55,976 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC000001: Failed to start service jboss.deployment.unit."rest-test.war".INSTALL: org.jboss.msc.service.StartException in service jboss.deployment.unit."rest-test.war".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of deployment "rest-test.war"
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:154)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: WFLYEE0041: Component class entities.Users for component Users has errors:
WFLYJPA0033: Can't find a persistence unit named null in deployment "rest-test.war"
at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor$1.handle(ModuleJndiBindingProcessor.java:157)
at org.jboss.as.ee.component.ClassDescriptionTraversal.run(ClassDescriptionTraversal.java:54)
at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor.processClassConfigurations(ModuleJndiBindingProcessor.java:186)
at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor.deploy(ModuleJndiBindingProcessor.java:143)
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:147)
... 5 more
16:40:55,978 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 2) WFLYCTL0013: Operation ("full-replace-deployment") failed - address: ([]) - failure description: {
"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"rest-test.war\".INSTALL" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"rest-test.war\".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of deployment \"rest-test.war\"
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: WFLYEE0041: Component class entities.Users for component Users has errors:
WFLYJPA0033: Can't find a persistence unit named null in deployment \"rest-test.war\""},
"WFLYCTL0180: Services with missing/unavailable dependencies" => [
"jboss.deployment.unit.\"rest-test.war\".batch.environment is missing [jboss.deployment.unit.\"rest-test.war\".beanmanager]",
"jboss.deployment.unit.\"rest-test.war\".weld.weldClassIntrospector is missing [jboss.deployment.unit.\"rest-test.war\".beanmanager]"
]
}
16:40:56,013 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0016: Replaced deployment "rest-test.war" with deployment "rest-test.war"
16:40:56,013 INFO [org.jboss.as.controller] (DeploymentScanner-threads - 2) WFLYCTL0183: Service status report
WFLYCTL0186: Services which failed to start: service jboss.deployment.unit."rest-test.war".INSTALL
service jboss.deployment.unit."rest-test.war".INSTALL: org.jboss.msc.service.StartException in service jboss.deployment.unit."rest-test.war".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of deployment "rest-test.war"
-
21. Re: Setting up Hibernate
mayerw01 Aug 2, 2016 11:52 AM (in response to royibernthal)I am not sure having understood you correctly. What are you doubts about stateless EJBs? But at least if you just follow the Specs the examples should work.
Regarding the messages above where did you place your persistence.xml? The file should be packages as described in Chap. 8.2 of the JPA Spec.
-
22. Re: Setting up Hibernate
royibernthal Aug 2, 2016 6:32 PM (in response to mayerw01)Is it possible to accomplish it without using stateless EJBs?
Am I not following the specs? Could you please help me see what I'm doing wrong?
I read chapter 8.2 - I tried following this point:
-the WEB-INF/classes directory of a WAR file
since my WEB-INF directory didn't contain a classes directory, I manually created a classes directory and placed inside a copy of persistence.xml. I must say I'm not entirely sure I know what I'm doing.
I also placed a copy of persistence.xml in WebContent/META-INF according to:
http://stackoverflow.com/questions/8276166/eclipselink-jpa-location-of-persistence-xml
In my case the folder name is META-INF but its path is not /src/main/resources/META-INF.
In addition I tried placing persistence.xml in other random places as shown in the screenshot, hoping one of them would hit the mark. It still doesn't work.
-
23. Re: Setting up Hibernate
mayerw01 Aug 3, 2016 6:54 AM (in response to royibernthal)On the WildFly home page you can read "WildFly implements the latest in enterprise Java standards" (which is currently Java EE7).
In the spec JSR 318 which covers Enterprise JavaBeans you can read: The Enterprise JavaBeans architecture is the standard component architecture for building object-oriented business applications in the Java programming language.
An enterprise bean typically contains business logic that operates on the enterprise’s data.
Therefore when developing applications for WildFly I'd strongly recommend to get yourself familiar with the Java EE7 technology.
Regarding your persistence issue you've attached the source printout. But it is important how the war is packaged. It could look like below.
-
24. Re: Setting up Hibernate
royibernthal Aug 3, 2016 6:52 PM (in response to mayerw01)I will, thanks for the detailed answer.
Here's how my war is packaged - I couldn't find a way to display the war content in a tree view like you did so I did it manually:
rest-test.war
- META-INF
- MANIFEST.MF
- persistence.xml
- WEB-INF
- classes
- entities
- User.class
- Users.class
- JAXActivator.class
- MyService.class
- Page.class
- persistence.xml
- entities
- lib
- web.xml
- classes
- index.html
- META-INF
-
25. Re: Setting up Hibernate
amostech Aug 4, 2016 12:04 AM (in response to royibernthal)To configure the driver you need to copy the jar and xml files that you download from mysql website. It is the JDBC connector and place it into the WILDFLY_HOME/modules/system/layers/base/com/mysql/main.
So this folder should end up having:
mysql-connector-X.X.XX-bin.jar
module.xml
That's it. After that. In your standalone.xml configuration you should add the following to your DRIVERS section within the datasources subsystem to enable the driver:
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="com.mysql" module="com.mysql">
<driver-class>com.mysql.jdbc.Driver</driver-class>
</driver>
</drivers>
-
26. Re: Setting up Hibernate
mayerw01 Aug 4, 2016 5:39 AM (in response to royibernthal)To display this tree I used the ark utility (https://www.kde.org/applications/utilities/). But there are certainly many other tools that can also be used for this purpose.
Even the standard java jar program provides such a function (just enter 'jar tf rest-test.war' in a comand window)
As mentioned in the JPA Spec for WAR files the persistence.xml should be either in
- the WEB-INF/classes directory of a WAR file
The root of the persistence unit is the WEB-INF/classes directory;
the persistence.xml file is therefore contained in the WEB-INF/classes/META-INF directory.
or - a jar file in the WEB-INF/lib directory of a WAR file
It is therefore wrong to place it in the root directory of the war file or directly into the classes directory
- the WEB-INF/classes directory of a WAR file
-
27. Re: Setting up Hibernate
royibernthal Aug 4, 2016 6:54 AM (in response to mayerw01)I understand. Where should I place persistence.xml in my source so that it'd appear in the WEB-INF/classes directory of the WAR file? Or am I expected to manually place it in the WAR file?
-
28. Re: Setting up Hibernate
mayerw01 Aug 4, 2016 8:31 AM (in response to royibernthal)This is controlled by the build tool (usually Ant or Maven).
Please check how this can be configured in your IDE.
-
29. Re: Setting up Hibernate
royibernthal Aug 4, 2016 6:35 PM (in response to mayerw01)I'm not using not Ant and not Maven (as far as I know), my IDE is Eclipse.
According to the link below I need to configure it via Project->Properties->Build Path - I have no idea what I should actually do there.
Another comment suggests I should place it in src/META-INF which doesn't exist (perhaps because of my project type), what I do have is Web Content/META-INF, where I did place my persistence.xml and still it doesn't appear in the right place in the compiled war.
jpa - Persistence.xml where to put in eclipse project - Stack Overflow