Add Department entity and basic tests

parent 340f8035
/target
.classpath
.project
.settings
package dgpena.siexample.persistence;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Department {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<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="si-database" transaction-type="RESOURCE_LOCAL">
<class>dgpena.siexample.persistence.Department</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="siuser" />
<property name="javax.persistence.jdbc.password" value="sipass" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/si" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
<property name="hibernate.connection.autocommit" value="false" />
<!-- alternatively to <class> and <property> declarations, you can use
a regular hibernate.cfg.xml file -->
<!-- property name="hibernate.ejb.cfgfile" value="/org/hibernate/ejb/test/hibernate.cfg.xml"/ -->
</properties>
</persistence-unit>
</persistence>
package dgpena.siexample.persistence;
import static org.junit.Assert.assertEquals;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.BeforeClass;
import org.junit.Test;
public class DepartmentTest extends SQLBasedTest {
private static EntityManagerFactory emf;
@BeforeClass
public static void setUpEntityManagerFactory() {
emf = Persistence.createEntityManagerFactory("si-database");
}
@Test
public void testCreateDepartment() throws Exception {
EntityManager em = emf.createEntityManager();
Department d = new Department();
d.setName("finanzas");
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
// check in the DB using JDBC
int deptId = d.getId();
Statement statement = jdbcConnection.createStatement();
ResultSet rs = statement.executeQuery("SELECT COUNT(*) as total FROM Department d where d.id = "+deptId);
rs.next();
assertEquals(1, rs.getInt("total"));
}
@Test
public void testFindDepartment() throws SQLException {
// insert a department previously with JDBC
Statement statement = jdbcConnection.createStatement();
statement.executeUpdate("INSERT INTO Department(name) values('finanzas')", Statement.RETURN_GENERATED_KEYS);
int deptId = getLastInsertedId(statement);
EntityManager em = emf.createEntityManager();
Department d = em.find(Department.class, deptId);
assertEquals(deptId, d.getId());
assertEquals("finanzas", d.getName());
}
}
package dgpena.siexample.persistence;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLBasedTest {
private static final String DB_URL = "jdbc:mysql://localhost:3306/si";
private static final String DB_USER = "siuser";
private static final String DB_PASS = "sipass";
protected static Connection jdbcConnection = createConnection();
private static Connection createConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
protected int getLastInsertedId(Statement statement) throws SQLException {
ResultSet rs = statement.getGeneratedKeys();
rs.next();
return rs.getInt(1);
}
}
<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="si-database" transaction-type="RESOURCE_LOCAL">
<class>dgpena.siexample.persistence.Department</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="siuser" />
<property name="javax.persistence.jdbc.password" value="sipass" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/si" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
<property name="hibernate.connection.autocommit" value="false" />
<!-- alternatively to <class> and <property> declarations, you can use
a regular hibernate.cfg.xml file -->
<!-- property name="hibernate.ejb.cfgfile" value="/org/hibernate/ejb/test/hibernate.cfg.xml"/ -->
</properties>
</persistence-unit>
</persistence>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment