Commit 26ea0fa6 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+617 −0

File added.

Preview size limit exceeded, changes collapsed.

+39 −0
Changes for Hibernate and Java Persistence API (JPA) Fundamentals - Working Files/Chapter 10/Expressions and Operators/pom.xml: 39 added lines, 0 removed lines.
Original line number Diff line number Diff line
<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.infiniteskills</groupId>
  <artifactId>hibernate-course</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>hibernate-course</name>
  <dependencies>
  	<dependency>
  		<groupId>org.hibernate</groupId>
  		<artifactId>hibernate-core</artifactId>
  		<version>4.3.6.Final</version>
  	</dependency>
  	<dependency>
  		<groupId>org.hibernate</groupId>
  		<artifactId>hibernate-annotations</artifactId>
  		<version>3.5.6-Final</version>
  	</dependency>
  	<dependency>
  		<groupId>org.slf4j</groupId>
  		<artifactId>slf4j-api</artifactId>
  		<version>1.7.7</version>
  	</dependency>
  	<dependency>
  		<groupId>org.slf4j</groupId>
  		<artifactId>slf4j-log4j12</artifactId>
  		<version>1.7.7</version>
  	</dependency>
  	<dependency>
  		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  		<version>5.1.33</version>
  	</dependency>
  	<dependency>
  		<groupId>org.hibernate</groupId>
  		<artifactId>hibernate-entitymanager</artifactId>
  		<version>4.3.7.Final</version>
  	</dependency>
  </dependencies>
</project>
 No newline at end of file
+149 −0
Changes for Hibernate and Java Persistence API (JPA) Fundamentals - Working Files/Chapter 10/Expressions and Operators/populate.sql: 149 added lines, 0 removed lines.
Original line number Diff line number Diff line
SET FOREIGN_KEY_CHECKS = 0;

TRUNCATE account;
TRUNCATE bank;
TRUNCATE bank_contact;
TRUNCATE budget;
TRUNCATE budget_transaction;
TRUNCATE credential;
TRUNCATE finances_user;
TRUNCATE ifinances_keys;
TRUNCATE time_test;
TRUNCATE transaction;
TRUNCATE user_account;
TRUNCATE user_address;

SET FOREIGN_KEY_CHECKS = 1;

INSERT INTO `ifinances`.`bank`
(`BANK_ID`,
`NAME`,
`ADDRESS_LINE_1`,
`ADDRESS_LINE_2`,
`CITY`,
`STATE`,
`ZIP_CODE`,
`IS_INTERNATIONAL`,
`LAST_UPDATED_BY`,
`LAST_UPDATED_DATE`,
`CREATED_BY`,
`CREATED_DATE`,
`ADDRESS_TYPE`)
VALUES
(null,
'Second National Trust',
'301 Snowy Lane',
'Suite 2',
'Adams',
'PA',
'10003',
0,
'system',
'2014-11-1',
'system',
'2014-11-1',
'PRIMARY');

INSERT INTO `ifinances`.`bank`
(`BANK_ID`,
`NAME`,
`ADDRESS_LINE_1`,
`ADDRESS_LINE_2`,
`CITY`,
`STATE`,
`ZIP_CODE`,
`IS_INTERNATIONAL`,
`LAST_UPDATED_BY`,
`LAST_UPDATED_DATE`,
`CREATED_BY`,
`CREATED_DATE`,
`ADDRESS_TYPE`)
VALUES
(null,
'Third National Trust',
'Sunrise Drive',
'Suite 22',
'Jonestown',
'MI',
'12303',
0,
'system',
'2014-11-1',
'system',
'2014-11-1',
'PRIMARY');

INSERT INTO `ifinances`.`bank`
(`BANK_ID`,
`NAME`,
`ADDRESS_LINE_1`,
`ADDRESS_LINE_2`,
`CITY`,
`STATE`,
`ZIP_CODE`,
`IS_INTERNATIONAL`,
`LAST_UPDATED_BY`,
`LAST_UPDATED_DATE`,
`CREATED_BY`,
`CREATED_DATE`,
`ADDRESS_TYPE`)
VALUES
(null,
'Third National Trust',
'Bellvue',
'Suite 2',
'Newport',
'RI',
'18943',
0,
'system',
'2014-11-1',
'system',
'2014-11-1',
'PRIMARY');

INSERT INTO `ifinances`.`bank`
(`BANK_ID`,
`NAME`,
`ADDRESS_LINE_1`,
`ADDRESS_LINE_2`,
`CITY`,
`STATE`,
`ZIP_CODE`,
`IS_INTERNATIONAL`,
`LAST_UPDATED_BY`,
`LAST_UPDATED_DATE`,
`CREATED_BY`,
`CREATED_DATE`,
`ADDRESS_TYPE`)
VALUES
(null,
'Forth National Trust',
'403 Park Ave',
'Suite 221',
'Madison',
'WI',
'10003',
0,
'system',
'2014-11-1',
'system',
'2014-11-1',
'PRIMARY');

INSERT INTO `account` VALUES (1,1,'CHECKING','Checking Account',1000.00,1000.00,'2014-12-04','2014-12-04','Kevin Bowersox','2014-12-04 17:30:30','Kevin Bowersox','2014-12-04 17:30:30');


INSERT INTO `transaction` VALUES (1,1,'Withdrawl','Dress Belt',50.00,1000.00,950.00,'New Dress Belt','Kevin Bowersox','2014-12-06 10:53:30','Kevin Bowersox','2014-12-06 10:53:30'),
                                 (2,1,'Withdrawl','Work Shoes',100.00,950.00,850.00,'Nice Pair of Shoes','Kevin Bowersox','2014-12-07 10:53:30','Kevin Bowersox','2014-12-06 10:53:30'),
                                 (3,1,'Withdrawl','Shirt',100.00,850.00,750.00,'Nice Shirt','Kevin Bowersox','2014-12-08 10:53:30','Kevin Bowersox','2014-12-06 10:53:30'),
                                 (4,1,'Withdrawl','Socks',10.00,750.00,740.00,'Nice Socks','Kevin Bowersox','2014-12-09 10:53:30','Kevin Bowersox','2014-12-06 10:53:30'),
                                 (5,1,'Withdrawl','Tie',40.00,740.00,700.00,'Nice Tie','Kevin Bowersox','2014-12-09 10:53:30','Kevin Bowersox','2014-12-06 10:53:30'),
                                 (6,1,'Withdrawl','Pants',100.00,700.00,600.00,'Nice Pants','Kevin Bowersox','2014-12-10 10:53:30','Kevin Bowersox','2014-12-06 10:53:30'),
                                 (7,1,'Withdrawl','Lunch',20.00,600.00,580.00,'Nice Lunch','Kevin Bowersox','2014-12-11 10:53:30','Kevin Bowersox','2014-12-06 10:53:30'),
                                 (8,1,'Withdrawl','Dinner',30.00,580.00,550.00,'Great Dinner','Kevin Bowersox','2014-12-11 10:53:30','Kevin Bowersox','2014-12-06 10:53:30'),
                                 (9,1,'Withdrawl','Breakfast',10.00,550.00,540.00,'Breakfast at Tiffanys','Kevin Bowersox','2014-12-12 10:53:30','Kevin Bowersox','2014-12-06 10:53:30'),
                                 (10,1,'Withdrawl','Groceries',100.00,540.00,440.00,'Healthy Food','Kevin Bowersox','2014-12-13 10:53:30','Kevin Bowersox','2014-12-06 10:53:30'),
                                 (11,1,'Deposit','Pay Check',1000.00,440.00,1440.00,'Pay Day','Kevin Bowersox','2014-12-14 10:53:30','Kevin Bowersox','2014-12-06 10:53:30'),
                                 (12,1,'Deposit','Bonus',500.00,1440.00,1940.00,'Annual Bonus','Kevin Bowersox','2014-12-19 10:53:30','Kevin Bowersox','2014-12-06 10:53:30');
+166 −0
Changes for Hibernate and Java Persistence API (JPA) Fundamentals - Working Files/Chapter 10/Expressions and Operators/src/main/java/com/infiniteskills/data/Application.java: 166 added lines, 0 removed lines.
Original line number Diff line number Diff line
package com.infiniteskills.data;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.infiniteskills.data.entities.Account;
import com.infiniteskills.data.entities.Address;
import com.infiniteskills.data.entities.Bank;
import com.infiniteskills.data.entities.Bond;
import com.infiniteskills.data.entities.Credential;
import com.infiniteskills.data.entities.Stock;
import com.infiniteskills.data.entities.Transaction;
import com.infiniteskills.data.entities.User;
import com.infiniteskills.data.entities.UserCredentialView;

public class Application {

	public static void main(String[] args) {		
		SessionFactory sessionFactory = null;
		Session session = null;
		org.hibernate.Transaction tx = null;

		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			tx = session.beginTransaction();

			UserCredentialView view = (UserCredentialView) session.get(UserCredentialView.class, 1L);
			System.out.println(view.getFirstName());
			System.out.println(view.getLastName());
			
			tx.commit();

		} catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		} finally {
			session.close();
			sessionFactory.close();
		}
	}

	protected static Bond createBond() {
		Bond bond = new Bond();
		bond.setInterestRate(new BigDecimal("123.22"));
		bond.setIssuer("JP Morgan Chase");
		bond.setMaturityDate(new Date());
		bond.setPurchaseDate(new Date());
		bond.setName("Long Term Bond Purchases");
		bond.setValue(new BigDecimal("10.22"));
		return bond;
	}

	protected static Stock createStock(){
		Stock stock = new Stock();
		stock.setIssuer("Allen Edmonds");
		stock.setName("Private American Stock Purchases");
		stock.setPurchaseDate(new Date());
		stock.setQuantity(new BigDecimal("1922"));
		stock.setSharePrice(new BigDecimal("100.00"));
		return stock;
	}
	
	protected static Bank createBank() {
		Bank bank = new Bank();
		bank.setName("First United Federal");
		bank.setAddressLine1("103 Washington Plaza");
		bank.setAddressLine2("Suite 332");
		bank.setAddressType("PRIMARY");
		bank.setCity("New York");
		bank.setCreatedBy("Kevin Bowersox");
		bank.setCreatedDate(new Date());
		bank.setInternational(false);
		bank.setLastUpdatedBy("Kevin Bowersox");
		bank.setLastUpdatedDate(new Date());
		bank.setState("NY");
		bank.setZipCode("10000");
		return bank;
	}

	protected static User createUser() {
		User user = new User();
		Address address = createAddress();
		user.setAddresses(Arrays.asList(new Address[] { createAddress() }));
		user.setBirthDate(new Date());
		user.setCreatedBy("Kevin Bowersox");
		user.setCreatedDate(new Date());
		user.setCredential(createCredential(user));
		user.setEmailAddress("test@test.com");
		user.setFirstName("John");
		user.setLastName("Doe");
		user.setLastUpdatedBy("system");
		user.setLastUpdatedDate(new Date());
		return user;
	}

	protected static Credential createCredential(User user) {
		Credential credential = new Credential();
		credential.setUser(user);
		credential.setUsername("test_username");
		credential.setPassword("test_password");
		return credential;
	}

	protected static Address createAddress() {
		Address address = new Address();
		address.setAddressLine1("101 Address Line");
		address.setAddressLine2("102 Address Line");
		address.setCity("New York");
		address.setState("PA");
		address.setZipCode("10000");
		address.setAddressType("PRIMARY");
		return address;
	}

	protected static Transaction createNewBeltPurchase(Account account) {
		Transaction beltPurchase = new Transaction();
		beltPurchase.setAccount(account);
		beltPurchase.setTitle("Dress Belt");
		beltPurchase.setAmount(new BigDecimal("50.00"));
		beltPurchase.setClosingBalance(new BigDecimal("0.00"));
		beltPurchase.setCreatedBy("Kevin Bowersox");
		beltPurchase.setCreatedDate(new Date());
		beltPurchase.setInitialBalance(new BigDecimal("0.00"));
		beltPurchase.setLastUpdatedBy("Kevin Bowersox");
		beltPurchase.setLastUpdatedDate(new Date());
		beltPurchase.setNotes("New Dress Belt");
		beltPurchase.setTransactionType("Debit");
		return beltPurchase;
	}

	protected static Transaction createShoePurchase(Account account) {
		Transaction shoePurchase = new Transaction();
		shoePurchase.setAccount(account);
		shoePurchase.setTitle("Work Shoes");
		shoePurchase.setAmount(new BigDecimal("100.00"));
		shoePurchase.setClosingBalance(new BigDecimal("0.00"));
		shoePurchase.setCreatedBy("Kevin Bowersox");
		shoePurchase.setCreatedDate(new Date());
		shoePurchase.setInitialBalance(new BigDecimal("0.00"));
		shoePurchase.setLastUpdatedBy("Kevin Bowersox");
		shoePurchase.setLastUpdatedDate(new Date());
		shoePurchase.setNotes("Nice Pair of Shoes");
		shoePurchase.setTransactionType("Debit");
		return shoePurchase;
	}

	protected static Account createNewAccount() {
		Account account = new Account();
		account.setCloseDate(new Date());
		account.setOpenDate(new Date());
		account.setCreatedBy("Kevin Bowersox");
		account.setInitialBalance(new BigDecimal("50.00"));
		account.setName("Savings Account");
		account.setCurrentBalance(new BigDecimal("100.00"));
		account.setLastUpdatedBy("Kevin Bowersox");
		account.setLastUpdatedDate(new Date());
		account.setCreatedDate(new Date());
		return account;
	}

}
+29 −0
Changes for Hibernate and Java Persistence API (JPA) Fundamentals - Working Files/Chapter 10/Expressions and Operators/src/main/java/com/infiniteskills/data/HibernateUtil.java: 29 added lines, 0 removed lines.
Original line number Diff line number Diff line
package com.infiniteskills.data;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

	private static final SessionFactory sessionFactory = buildSessionFactory();

	private static SessionFactory buildSessionFactory() {
		try {
			Configuration configuration = new Configuration();
			configuration.configure();
			return configuration
					.buildSessionFactory(new StandardServiceRegistryBuilder()
							.applySettings(configuration.getProperties())
							.build());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(
					"There was an error building the factory");
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
}
Loading