pb00067,
Sorry I didn't respond sooner. I had a bunch of things I had to do. Additionally, I had a very difficult time getting HSQLDB or Derby to work correctly and decided to use MySQL. These are for quick templates and so there is an appeal to have an embedded DB for a template of course.
Tomcat also presents some challenges where it doesn't seem to like the DB driver anywhere but in it's lib directory (doesn't work when it's in the web app lib sub directory). Anyways, I did get it working with the persistence.xml like you said and with annotations. I thank you for your help in this and suggestions to persevere. Thanks for your time, definitely. :)
In the end, my persistence.xml became:
Code:
<persistence version="1.0" 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_1_0.xsd">
<!-- Adapted from: https://www.hibernate.org/114.html, Hibernate Web site -->
<persistence-unit name="fbh" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.default_schema" value="fbh" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/fbh" />
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<!-- Use the C3P0 connection pool. -->
<property name="c3p0.min_size" value="3" />
<property name="c3p0.max_size" value="5" />
<property name="c3p0.timeout" value="1800" />
<!-- Disable second-level cache. -->
<property name="cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
<property name="cache.use_query_cache" value="false" />
<property name="cache.use_minimal_puts" value="false" />
<property name="max_fetch_depth" value="3" />
<!-- Print SQL to stdout. -->
<property name="show_sql" value="true" />
<property name="format_sql" value="true" />
<!-- Drop and then re-create schema on SessionFactory build, for testing. -->
<property name="hibernate.hbm2ddl.auto" value="create" />
<!-- Bind the getCurrentSession() method to the thread. -->
<property name="current_session_context_class" value="thread" />
</properties>
</persistence-unit>
</persistence>
My annotated class was:
Code:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.validator.Length;
import org.hibernate.validator.Min;
import org.hibernate.validator.Max;
import org.hibernate.validator.NotEmpty;
@Entity
@Table(name="user")
public class User implements Serializable {
private static final long serialVersionUID = 1881413500711441951L;
public User() {
firstName = "test";
lastName = "data";
}
public User(String first, String last) {
firstName = first;
lastName = last;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, updatable=false)
private long id;
@Column(name = "lastName", nullable = false, unique = false)
private String lastName;
@Column(name = "firstName", nullable = false, unique = false)
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String toString() {
return firstName + " " + lastName;
}
}
And my servlet (for simple testing) became:
Code:
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletOutputStream;
import javax.servlet.UnavailableException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.apache.log4j.Logger;
public class Loader extends HttpServlet {
private static final String PERSISTENCE_UNIT = "fbh";
private static Logger logger = Logger.getLogger(Loader.class);
private static EntityManager em = null;
static {
// get entity manager via factory
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
em = entityManagerFactory.createEntityManager();
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// https://www.hibernate.org/42.html
// http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html_single/
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
em.merge(new User());
tx.commit();
} catch (Exception e) {
logger.error("APP PERSISTING ERROR: " + e.getMessage());
tx.rollback();
} finally {
//logger.info("APP CLOSING ENTITY MANAGER.");
//em.close();
}
ServletOutputStream out = res.getOutputStream();
out.println("no exceptions? Good. :)");
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
I post the above in the hopes it may be usable to others who come across configuration issues when approaching Hibernate without say a managed persistence manager. I'll mark this as "SOLVED".