我已经为Cassandra
数据库编写了一个基本的示例Spring-Hibernate应用程序
1)人员-模型班
2)PersonDao-道课
3)hbm文件-用于映射
4)applicationContext.xml
5)PersonMain-主类
6)pom.xml
7)异常信息
人员模型类:-
public class Person {
private int personId;
private String personName;
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
}
PersonDao:-此类用于数据库操作
public class PersonDao {
HibernateTemplate template;
public void setTemplate(HibernateTemplate template){
this.template = template;
}
public void savePerson(Person p){
template.save(p);
}
public void updatePerson(Person p){
template.update(p);
}
public void deletePerson(Person p){
template.delete(p);
}
public Person getPersonById(int personId){
return (Person)template.get(Person.class, personId);
}
public List<Person> getAllPersons(){
return template.loadAll(Person.class);
}
}
person.hbm.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.sample.spring.bean.Person" table="person">
<id name="personId" column="person_id"/>
<property name="personName" column="person_name"/>
</class>
</hibernate-mapping>
applicationContect.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="org.apache.cassandra.cql.jdbc.CassandraDriver" />
<property name="url" value="jdbc:cassandra://localhost:9160/user" />
<!-- <property name="username" value="cassandra"/>
<property name="password" value="cassandra"/> -->
</bean>
<bean id="mysessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="ds" />
<property name="mappingResources">
<list>
<value>person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.ogm.dialect.cassandra.CassandraCQL2Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="mysessionFactory"/>
</bean>
<bean id="d" class="com.sample.spring.dao.PersonDao">
<property name="template" ref="hibernateTemplate"></property>
</bean>
</beans>
PersonMain:-
public class PersonMain {
public static void main(String[] args) {
System.out.println("****** Spring With Hibernate *******");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonDao dao = context.getBean(PersonDao.class);
System.out.println("****** Starting Dao Operations *******");
Person p = new Person();
p.setPersonId(126);
p.setPersonName("Sathish");
Person p2 = new Person();
p2.setPersonId(124);
p2.setPersonName("Sathish Kumar");
// This statement getting executed and I am able to see record in the DB
dao.savePerson(p);
// After insertion the below print statement is not printing in console and also not executing next statements too
// i.e after insertion closing the connection
System.out.println("****** End of Dao Insert *******");
List<Person> list = dao.getAllPersons();
list.forEach(person -> {System.out.println(person);});
System.out.println("****** End of Dao Get All *******");
Person p1 = dao.getPersonById(123);
System.out.println(p1);
System.out.println("****** End of Dao Get *******");
dao.updatePerson(p1);
System.out.println("****** End of Dao Update *******");
dao.deletePerson(p2);
System.out.println("****** End of Dao Delete *******");
}
}
Pom.xml:-
<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.sample.spring</groupId>
<artifactId>Sample-Spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Sample-Spring</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-version>3.0.4.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- AOP dependency -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.7.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.0.GA</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.apache.cassandra</groupId>
<artifactId>cassandra-all</artifactId>
<version>3.11.2</version>
</dependency>
<dependency>
<groupId>org.apache-extras.cassandra-jdbc</groupId>
<artifactId>cassandra-jdbc</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-cassandra</artifactId>
<version>5.2.0.Alpha1</version>
</dependency>
</dependencies>
异常信息:-
现在我得到java.lang.ClassNotFoundException: org.hibernate.ogm.dialect.cassandra.CassandraCQL2Dialect
,为此也添加了依赖性,但仍然解决了这个问题