Witam, kupiłem książkę Spring Framework. Wprowadzenie do tworzenia aplikacji. Wydanie II natomiast chciałem sprawdzić jak działa pewien przykład z tej książki i uzyłem kodu dołaczonego do książki , niestety nie wiem czemu ale są błedy, dopiero się uczę Springa. Czy ktoś wie jak to naprawić? Całe repo(rozdzial7) jest pod adresem http://helion.pl/ksiazki/spring-framework-wprowadzenie-do-tworzenia-aplikacji-wydanie-ii-j-sharma-ashish-sarin,spfrwp.htm#szczegoly
INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@69fed1f3: startup date [Wed Dec 16 16:54:04 CET 2015]; root of context hierarchy
INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [META-INF/spring/applicationContext.xml]
INFO org.springframework.context.support.PropertySourcesPlaceholderConfigurer - Loading properties file from URL [file:/E:/SPRING/Kody/Rozdzial07/ch07-bankapp-hibernate/target/classes/META-INF/spring/database.properties]
INFO org.hibernate.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
INFO org.hibernate.Version - HHH000412: Hibernate Core {4.3.0.Final}
INFO org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
INFO org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist
WARN org.hibernate.engine.jdbc.internal.JdbcServicesImpl - HHH000342: Could not obtain connection to query metadata : Cannot create PoolableConnectionFactory (Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.)
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bankAccountDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory sample.spring.chapter07.bankapp.dao.BankAccountDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [META-INF/spring/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at sample.spring.chapter07.bankapp.BankApp.main(BankApp.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory sample.spring.chapter07.bankapp.dao.BankAccountDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [META-INF/spring/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 18 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [META-INF/spring/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 20 more
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:205)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:339)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:427)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:412)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 30 more
package sample.spring.chapter07.bankapp.dao;
import sample.spring.chapter07.bankapp.domain.BankAccountDetails;
public interface BankAccountDao {
int createBankAccount(BankAccountDetails bankAccountDetails);
void subtractFromAccount(int bankAccountId, int amount);
}
package sample.spring.chapter07.bankapp.dao;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import sample.spring.chapter07.bankapp.domain.BankAccountDetails;
@Repository(value = "bankAccountDao")
public class BankAccountDaoImpl implements BankAccountDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public int createBankAccount(final BankAccountDetails bankAccountDetails) {
sessionFactory.getCurrentSession().save(bankAccountDetails);
return bankAccountDetails.getAccountId();
}
@Override
public void subtractFromAccount(int bankAccountId, int amount) {
String hql = "from BankAccountDetails as bankAccountDetails where bankAccountDetails.accountId ="
+ bankAccountId;
BankAccountDetails bankAccountDetails = (BankAccountDetails) sessionFactory
.getCurrentSession().createQuery(hql).uniqueResult();
bankAccountDetails.setBalanceAmount(bankAccountDetails
.getBalanceAmount() - amount);
sessionFactory.getCurrentSession().merge(bankAccountDetails);
}
}
package sample.spring.chapter07.bankapp.dao;
import sample.spring.chapter07.bankapp.domain.FixedDepositDetails;
public interface FixedDepositDao {
int createFixedDeposit(FixedDepositDetails fdd);
FixedDepositDetails getFixedDeposit(int fixedDepositId);
}
package sample.spring.chapter07.bankapp.dao;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import sample.spring.chapter07.bankapp.domain.FixedDepositDetails;
@Repository(value = "fixedDepositDao")
public class FixedDepositDaoImpl implements FixedDepositDao {
@Autowired
@Qualifier("sessionFactory")
private SessionFactory sessionFactory;
public int createFixedDeposit(final FixedDepositDetails fdd) {
sessionFactory.getCurrentSession().save(fdd);
return fdd.getFixedDepositId();
}
public FixedDepositDetails getFixedDeposit(final int fixedDepositId) {
String hql = "from FixedDepositDetails as fixedDepositDetails where fixedDepositDetails.fixedDepositId ="
+ fixedDepositId;
return (FixedDepositDetails) sessionFactory.getCurrentSession()
.createQuery(hql).uniqueResult();
}
}
package sample.spring.chapter07.bankapp.domain;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity(name = "BankAccountDetails")
@Table(name = "bank_account_details")
public class BankAccountDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "account_id")
private int accountId;
@Column(name = "balance_amount")
private int balanceAmount;
@Column(name = "last_transaction_ts")
private Date lastTransactionTimestamp;
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
public int getBalanceAmount() {
return balanceAmount;
}
public void setBalanceAmount(int balanceAmount) {
this.balanceAmount = balanceAmount;
}
public Date getLastTransactionTimestamp() {
return lastTransactionTimestamp;
}
public void setLastTransactionTimestamp(Date lastTransactionTimestamp) {
this.lastTransactionTimestamp = lastTransactionTimestamp;
}
@Override
public boolean equals(Object otherObject) {
BankAccountDetails otherBankAccountDetails = (BankAccountDetails) otherObject;
if (otherBankAccountDetails.getAccountId() == this.accountId) {
return true;
} else {
return false;
}
}
}
package sample.spring.chapter07.bankapp.domain;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity(name = "FixedDepositDetails")
@Table(name = "fixed_deposit_details")
public class FixedDepositDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "fixed_deposit_id")
private int fixedDepositId;
@ManyToOne
@JoinColumn(name = "ACCOUNT_ID", referencedColumnName = "ACCOUNT_ID", nullable = false)
private BankAccountDetails bankAccountId;
@Column(name = "fd_creation_date")
private Date fdCreationDate;
@Column(name = "amount")
private int fdAmount;
@Column(name = "tenure")
private int tenure;
@Column(name = "active")
private String active;
public int getFixedDepositId() {
return fixedDepositId;
}
public void setFixedDepositId(int fixedDepositId) {
this.fixedDepositId = fixedDepositId;
}
public Date getFdCreationDate() {
return fdCreationDate;
}
public void setFdCreationDate(Date fdCreationDate) {
this.fdCreationDate = fdCreationDate;
}
public int getFdAmount() {
return fdAmount;
}
public void setFdAmount(int fdAmount) {
this.fdAmount = fdAmount;
}
public int getTenure() {
return tenure;
}
public void setTenure(int tenure) {
this.tenure = tenure;
}
public String getActive() {
return active;
}
public void setActive(String active) {
this.active = active;
}
public BankAccountDetails getBankAccountId() {
return bankAccountId;
}
public void setBankAccountId(BankAccountDetails bankAccountId) {
this.bankAccountId = bankAccountId;
}
@Override
public String toString() {
return "FixedDepositDetails [fixedDepositId=" + fixedDepositId
+ ", bankAccountId=" + bankAccountId + ", fdCreationDate="
+ fdCreationDate + ", fdAmount=" + fdAmount + ", tenure="
+ tenure + ", active=" + active + "]";
}
@Override
public boolean equals(Object obj) {
FixedDepositDetails other = (FixedDepositDetails) obj;
if (other.getFixedDepositId() == this.fixedDepositId) {
return true;
} else {
return false;
}
}
}
package sample.spring.chapter07.bankapp;
import java.util.Date;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sample.spring.chapter07.bankapp.domain.BankAccountDetails;
import sample.spring.chapter07.bankapp.domain.FixedDepositDetails;
import sample.spring.chapter07.bankapp.service.BankAccountService;
import sample.spring.chapter07.bankapp.service.FixedDepositService;
public class BankApp {
private static Logger logger = Logger.getLogger(BankApp.class);
public static void main(String args[]) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:META-INF/spring/applicationContext.xml");
BankAccountService bankAccountService = context
.getBean(BankAccountService.class);
BankAccountDetails bankAccountDetails = new BankAccountDetails();
bankAccountDetails.setBalanceAmount(1000);
bankAccountDetails.setLastTransactionTimestamp(new Date());
int bankAccountId = bankAccountService
.createBankAccount(bankAccountDetails);
logger.info("Utworzono konto bankowe o identyfikatorze - " + bankAccountId);
FixedDepositService fixedDepositService = context
.getBean(FixedDepositService.class);
FixedDepositDetails fdd = new FixedDepositDetails();
fdd.setActive("Y");
fdd.setBankAccountId(bankAccountDetails);
fdd.setFdCreationDate(new Date());
fdd.setFdAmount(500);
fdd.setTenure(12);
int fixedDepositId = fixedDepositService.createFixedDeposit(fdd);
logger.info("Założono lokatę o identyfikatorze - " + fixedDepositId);
logger.info(fixedDepositService.getFixedDeposit(fixedDepositId));
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan base-package="sample.spring.chapter07.bankapp" />
<context:property-placeholder
location="classpath*:META-INF/spring/database.properties" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="sample.spring" />
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
# Dumping database structure for spring_bank_app_db
DROP DATABASE IF EXISTS `spring_bank_app_db`;
CREATE DATABASE IF NOT EXISTS `spring_bank_app_db` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `spring_bank_app_db`;
# Struktura tabeli spring_bank_app_db.bank_account_details
DROP TABLE IF EXISTS `bank_account_details`;
CREATE TABLE IF NOT EXISTS `bank_account_details` (
`ACCOUNT_ID` int(10) NOT NULL AUTO_INCREMENT,
`BALANCE_AMOUNT` float NOT NULL,
`LAST_TRANSACTION_TS` datetime NOT NULL,
PRIMARY KEY (`ACCOUNT_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Zawiera informacje o kontach bankowych użytkowników.';
# Struktura tabeli spring_bank_app_db.fixed_deposit_details
DROP TABLE IF EXISTS `fixed_deposit_details`;
CREATE TABLE IF NOT EXISTS `fixed_deposit_details` (
`FIXED_DEPOSIT_ID` int(10) NOT NULL AUTO_INCREMENT,
`ACCOUNT_ID` int(10) NOT NULL,
`FD_CREATION_DATE` date NOT NULL,
`AMOUNT` int(10) NOT NULL,
`TENURE` int(10) NOT NULL,
`ACTIVE` char(1) NOT NULL DEFAULT 'Y',
PRIMARY KEY (`FIXED_DEPOSIT_ID`),
KEY `FK_fixed_deposit_details_bank_account_details` (`ACCOUNT_ID`),
CONSTRAINT `FK_fixed_deposit_details_bank_account_details` FOREIGN KEY (`ACCOUNT_ID`) REFERENCES `bank_account_details` (`ACCOUNT_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;