Monday, March 3, 2014

persistence.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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="BookJpaPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <!--
                value='create' to build a new database on each run;
                value='update' to modify an existing database;
                value='create-drop' means the same as 'create' but also drops tables when Hibernate closes;
                value='validate' makes no changes to the database
             -->
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.use_sql_comments" value="true" />
         </properties>
    </persistence-unit>

</persistence>

Wednesday, October 30, 2013

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:amq="http://activemq.apache.org/schema/core"
  xmlns:jms="http://www.springframework.org/schema/jms"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
            http://www.springframework.org/schema/jdbc
            http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/jee
            http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
            http://activemq.apache.org/schema/core
            http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd
            http://www.springframework.org/schema/jms 
            http://www.springframework.org/schema/jms/spring-jms-2.5.xsd">

  <context:annotation-config />
  <context:component-scan base-package="bookstore" />
  <tx:annotation-driven />
  <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
  <bean id="dataSource1"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName"> <value>org.gjt.mm.mysql.Driver</value></property>
    <property name="url"> <value>jdbc:mysql://localhost/test</value> </property>
    <property name="username"><value>test1</value> </property>
    <property name="password"><value>*****</value></property>
  </bean>
  
  <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:dataSource-ref="dataSource1"
    p:persistenceUnitName="BookJpaPersistenceUnit"/>
  
  <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
  
  <bean id="transactionManager"
          class="org.springframework.orm.jpa.JpaTransactionManager"
          p:entityManagerFactory-ref="entityManagerFactory"/>
  
  <tx:annotation-driven transaction-manager="transactionManager"/>
    
</beans>

Thursday, June 20, 2013

Spring Hibernate: web/index.jsp


<meta http-equiv="refresh" content="1; url=/bookStore/addBook"/>

Spring Hibernate: bookstore.service.BookServiceImpl.java


package bookstore.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import bookstore.dao.BaseDao;
import bookstore.domain.Book;


@Service ("bookService")
public class BookServiceImpl implements BookService {

@Autowired
BaseDao baseDao;

@Override
public void persist(Book book) {
baseDao.persist(book);

}

@Override
public List<Book> getAll() {

return baseDao.getAll(Book.class);
}

}

Spring Hibernate: bookstore.service.BookService.java


package bookstore.service;

import java.util.List;

import bookstore.domain.Book;

public interface BookService {

public void persist(Book book);

public List<Book> getAll();
}

Spring Hibernate: bookstore.domain.Book.java


package bookstore.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;


@Entity (name = "book")
public class Book {

@Id
@GeneratedValue
private int bookId;

@NotNull
@Size (min = 2, max = 20)
private String title;

@DecimalMin (value = "2.0")
@DecimalMax (value = "100.0")
private double price;


public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}


}