persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<persistence-unit name="prod" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>webadmin.domain.WebSite</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/dummy?useUnicode=yes&characterEncoding=utf8;" />
<property name="hibernate.connection.username" value="root1" />
<property name="hibernate.connection.password" value="root1" />
</properties>
</persistence-unit>
Service
@Stateless
public class WebSiteService {
@PersistenceContext(unitName = "prod")
private EntityManager entityManager;
public WebSite saveWebSite(WebSite webSite) {
System.out.println("before query");
Query q=entityManager.createNativeQuery("insert into dum values (1, 'ws');");
System.out.println("before execution");
q.executeUpdate();
System.out.println("after execution");
Problem
I'm fighting an org.hsqldb.HsqlException: user lacks privilege or object not found: DUM
issue. While trying to find the root cause, I have updated my persistence.xml
with an invalid database name and invalid user/password. Yet, I see no exceptions related to this. So I believe that even using correct parameters, I simply never connect to the database. What's missing here? How can I check such connection is made?
TomEE 1.6, MySQL 5.
Solution
Going with JB's comment that TomEE requires a datasource to be defined in tomee.xml, so add the data source there.
<tomee>
<Resource id="prodDataSource" type="javax.sql.DataSource">
jdbcDriver com.mysql.jdbc.Driver
jdbcUrl jdbc:mysql://localhost:3306/yourdbname
jtaManaged true
password root
userName root
InitialSize 50
MaxActive 100
MaxIdle 3
</Resource>
</tomee>
No-one else has access to my server, so I'm keeping the credentials in tomee.xml. That way, my persistence.xml properties section is reduced to this:
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
</properties>
Remove project from the server. Double click on the server to go to server overview. Under server locations: choose Tomcat installation directory. Clean server, project, restart. Worked for me.
A valuable comment from RajV: How to define MySQL data source in TomEE?