Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 18, 2016
0 parents
commit 0a50412
Showing
120 changed files
with
6,867 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>hibernate-parent</artifactId> | ||
<groupId>com.redhat.osas.books.hibernate</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>chapter01</artifactId> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package chapter01.hibernate; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
public class Message { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
Long id; | ||
@Column(nullable = false) | ||
String text; | ||
|
||
public Message(String text) { | ||
setText(text); | ||
} | ||
|
||
public Message() { | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public void setText(String text) { | ||
this.text = text; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Message{" + | ||
"id=" + getId() + | ||
", text='" + getText() + '\'' + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package chapter01.pojo; | ||
|
||
public class Message { | ||
String text; | ||
|
||
public Message() { | ||
} | ||
|
||
public Message(String text) { | ||
setText(text); | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public void setText(String text) { | ||
this.text = text; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
chapter01/src/test/java/chapter01/hibernate/PersistenceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package chapter01.hibernate; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.Transaction; | ||
import org.hibernate.cfg.Configuration; | ||
import org.hibernate.service.ServiceRegistry; | ||
import org.hibernate.service.ServiceRegistryBuilder; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.List; | ||
|
||
public class PersistenceTest { | ||
SessionFactory factory; | ||
|
||
@BeforeClass | ||
public void setup() { | ||
Configuration configuration = new Configuration(); | ||
configuration.configure(); | ||
ServiceRegistryBuilder srBuilder = new ServiceRegistryBuilder(); | ||
srBuilder.applySettings(configuration.getProperties()); | ||
ServiceRegistry serviceRegistry = srBuilder.buildServiceRegistry(); | ||
factory = configuration.buildSessionFactory(serviceRegistry); | ||
} | ||
|
||
@Test | ||
public void saveMessage() { | ||
Message message = new Message("Hello, world"); | ||
Session session = factory.openSession(); | ||
Transaction tx = session.beginTransaction(); | ||
session.persist(message); | ||
tx.commit(); | ||
session.close(); | ||
} | ||
|
||
@Test(dependsOnMethods = "saveMessage") | ||
public void readMessage() { | ||
Session session = factory.openSession(); | ||
@SuppressWarnings("unchecked") | ||
List<Message> list = (List<Message>) session.createQuery("from Message").list(); | ||
|
||
if (list.size() > 1) { | ||
Assert.fail("Message configuration in error; table should contain only one." | ||
+ " Set ddl to create-drop."); | ||
} | ||
if (list.size() == 0) { | ||
Assert.fail("Read of initial message failed; check saveMessage() for errors." | ||
+ " How did this test run?"); | ||
} | ||
for (Message m : list) { | ||
System.out.println(m); | ||
} | ||
session.close(); | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
chapter01/src/test/java/chapter01/jdbc/PersistenceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package chapter01.jdbc; | ||
|
||
import chapter01.hibernate.Message; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeSuite; | ||
import org.testng.annotations.Test; | ||
|
||
import java.sql.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class PersistenceTest { | ||
@BeforeSuite | ||
public void setup() throws ClassNotFoundException { | ||
Class.forName("org.hsqldb.jdbc.JDBCDriver"); | ||
Connection connection = null; | ||
PreparedStatement ps = null; | ||
try { | ||
connection = DriverManager.getConnection("jdbc:hsqldb:db1;shutdown=true"); | ||
|
||
// clear out the old data, if any, so we know the state of the DB | ||
ps = connection.prepareStatement("DROP TABLE messages IF EXISTS"); | ||
ps.execute(); | ||
|
||
ps = connection.prepareStatement( | ||
"CREATE TABLE messages (" | ||
+ "id BIGINT GENERATED BY DEFAULT AS IDENTITY " | ||
+ "PRIMARY KEY, " | ||
+ "text VARCHAR(256))" | ||
); | ||
ps.execute(); | ||
|
||
// eagerly free resources | ||
ps.close(); | ||
connection.close(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
throw new RuntimeException(e); | ||
} finally { | ||
try { | ||
if (ps != null && !ps.isClosed()) { | ||
ps.close(); | ||
} | ||
} catch (SQLException ignored) { | ||
} | ||
try { | ||
if (connection != null && !connection.isClosed()) { | ||
connection.close(); | ||
} | ||
} catch (SQLException ignored) { | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void saveMessage() { | ||
Connection connection = null; | ||
PreparedStatement ps = null; | ||
try { | ||
connection = DriverManager.getConnection("jdbc:hsqldb:db1;shutdown=true"); | ||
connection.setAutoCommit(false); | ||
|
||
ps = connection.prepareStatement("INSERT INTO messages(text) VALUES (?)"); | ||
|
||
ps.setString(1, "Hello, World"); | ||
ps.execute(); | ||
connection.commit(); | ||
|
||
// eagerly free resources | ||
ps.close(); | ||
connection.close(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
throw new RuntimeException(e); | ||
} finally { | ||
try { | ||
if (ps != null && !ps.isClosed()) { | ||
ps.close(); | ||
} | ||
} catch (SQLException ignored) { | ||
} | ||
try { | ||
if (connection != null && !connection.isClosed()) { | ||
connection.close(); | ||
} | ||
} catch (SQLException ignored) { | ||
} | ||
} | ||
} | ||
|
||
@Test(dependsOnMethods = "saveMessage") | ||
public void readMessage() { | ||
Connection connection = null; | ||
PreparedStatement ps = null; | ||
ResultSet rs = null; | ||
List<Message> list = new ArrayList<>(); | ||
try { | ||
connection = DriverManager.getConnection("jdbc:hsqldb:db1;shutdown=true"); | ||
|
||
ps = connection.prepareStatement("SELECT id, text FROM messages"); | ||
|
||
rs = ps.executeQuery(); | ||
while (rs.next()) { | ||
Message message = new Message(); | ||
message.setId(rs.getLong(1)); | ||
message.setText(rs.getString(2)); | ||
list.add(message); | ||
} | ||
|
||
if (list.size() > 1) { | ||
Assert.fail("Message configuration in error; table should contain only one." | ||
+ " Set ddl to drop-create."); | ||
} | ||
if (list.size() == 0) { | ||
Assert.fail("Read of initial message failed; check saveMessage() for errors." | ||
+ " How did this test run?"); | ||
} | ||
for (Message m : list) { | ||
System.out.println(m); | ||
} | ||
// eagerly free resources | ||
rs.close(); | ||
ps.close(); | ||
connection.close(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
throw new RuntimeException(e); | ||
} finally { | ||
try { | ||
if (rs != null && !rs.isClosed()) { | ||
rs.close(); | ||
} | ||
} catch (SQLException ignored) { | ||
} | ||
try { | ||
if (ps != null && !ps.isClosed()) { | ||
ps.close(); | ||
} | ||
} catch (SQLException ignored) { | ||
} | ||
try { | ||
if (connection != null && !connection.isClosed()) { | ||
connection.close(); | ||
} | ||
} catch (SQLException ignored) { | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0"?> | ||
<!DOCTYPE hibernate-configuration PUBLIC | ||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" | ||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> | ||
<hibernate-configuration> | ||
<session-factory> | ||
<!-- Database connection settings --> | ||
<property name="connection.driver_class">org.hsqldb.jdbc.JDBCDriver</property> | ||
<property name="connection.url">jdbc:hsqldb:db1;shutdown=true</property> | ||
<property name="connection.username">sa</property> | ||
<property name="connection.password"/> | ||
<property name="dialect">org.hibernate.dialect.HSQLDialect</property> | ||
<!-- Echo all executed SQL to stdout --> | ||
<property name="show_sql">true</property> | ||
<!-- Drop and re-create the database schema on startup --> | ||
<property name="hbm2ddl.auto">create-drop</property> | ||
<mapping class="chapter01.hibernate.Message"/> | ||
</session-factory> | ||
|
||
</hibernate-configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>hibernate-parent</artifactId> | ||
<groupId>com.redhat.osas.books.hibernate</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>chapter02</artifactId> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package chapter02.hibernate; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
public class Message { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
Long id; | ||
@Column(nullable = false) | ||
String text; | ||
|
||
public Message(String text) { | ||
setText(text); | ||
} | ||
|
||
public Message() { | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public void setText(String text) { | ||
this.text = text; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Message{" + | ||
"id=" + getId() + | ||
", text='" + getText() + '\'' + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package chapter02.pojo; | ||
|
||
public class Message { | ||
String text; | ||
|
||
public Message() { | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public void setText(String text) { | ||
this.text = text; | ||
} | ||
} |
Oops, something went wrong.