Vielen Dank für eure Antworten :
Ich habe allerdings ein kleines Problem nach dem ersten Versuch.
Hier meine Konfiguration :
hibernate.cfg.xml
pom.xml
meine Java Class
Wenn ich die Klasse starte bekomme ich allerdings eine Exception :
Kann mir jemand helfen.
Ich habe allerdings ein kleines Problem nach dem ersten Versuch.
Hier meine Konfiguration :
hibernate.cfg.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.SQLiteDialect</property>
<property name="connection.driver_class">org.sqlite.JDBC</property>
<property name="connection.url">jdbc:sqlite:mydb.db</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="test.hibernate.User" />
</session-factory>
</hibernate-configuration>
pom.xml
Java:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.srccodes.example.hibernate</groupId>
<artifactId>HibernateHelloWorld</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>HibernateHelloWorld</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
</dependency>
<!-- SQLite JDBC library -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
<!-- junit test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
meine Java Class
Java:
public class Main {
private static SessionFactory sessionFactory = null;
private static ServiceRegistry serviceRegistry = null;
private static SessionFactory configureSessionFactory() throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
Properties properties = configuration.getProperties();
serviceRegistry = new ServiceRegistryBuilder().applySettings(properties).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
public static void main(String[] args) {
// Configure the session factory
configureSessionFactory();
Session session = null;
Transaction tx = null;
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
// Creating Contact entity that will be save to the sqlite database
User user = new User();
user.setStrFirstName("User1 - FirstName");
user.setStrLastName("User1 - LastName");
User user2 = new User();
user2.setStrFirstName("User2 - FirstName");
user2.setStrLastName("User2 - LastName");
// Saving to the database
session.save(user);
session.save(user2);
// Committing the change in the database.
session.flush();
tx.commit();
// Fetching saved data
List<User> userList = session.createQuery("from user").list();
for (User tmpUser : userList) {
System.out.println("Id: " + tmpUser.getIntUserID() + " | Name:" + tmpUser.getStrLastName() +
" | PreName:" + tmpUser.getStrFirstName());
}
} catch (Exception ex) {
ex.printStackTrace();
// Rolling back the changes to make the data consistent in case of any failure
// in between multiple database write operations.
tx.rollback();
} finally {
if (session != null) {
session.close();
}
}
}
}
Wenn ich die Klasse starte bekomme ich allerdings eine Exception :
Code:
org.hibernate.HibernateException: Dialect class not found: org.hibernate.dialect.SQLiteDialect
Kann mir jemand helfen.