.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.ExceptionInInitializerError
at UserApp.main(UserApp.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: org.hibernate.MappingException: Unable to load class [ entity.Article] declared in Hibernate configuration <mapping/> entry
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2369)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:242)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:71)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:230)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:71)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:212)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:71)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:206)
at dao.DAO.<clinit>(DAO.java:17)
... 6 more
Caused by: java.lang.ClassNotFoundException: entity.Article
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2366)
... 19 more
Process finished with exit code 1
import dao.UserDAO;
import entity.User;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
public class UserApp {
private static Logger logger = Logger.getLogger(UserApp.class.getName());
public static void main(String[] args) {
String userInput = ""; // Line read from standard in
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
String username = "";
try {
while (!(userInput.equals("0"))) {
System.out.println("1. Создать пользователя");
System.out.println("2. Найти пользователя");
System.out.println("3. Удалить пользователя");
System.out.println("0. Выход");
userInput = in.readLine();
if ("1".equals(userInput)) {
try {
System.out.print(" Введите имя пользователя: ");
username = in.readLine();
UserDAO userDAO = new UserDAO();
User user = userDAO.createUser(username, "1");
System.out.println("Пользователь создан. Имя: "
+ user.getName() + " пароль: " + user.getPassword());
} catch (Exception e) {
System.out.println("Пользователь " + username + " уже существует.");
}
} else if ("2".equals(userInput)) {
try {
System.out.print(" Введите имя пользователя: ");
username = in.readLine();
UserDAO userDAO = new UserDAO();
User user = userDAO.retrieveUser( username );
System.out.println( "Пользователь получен из базы данных. Имя: "
+ user.getName() + " пароль: " + user.getPassword());
} catch (Exception e) {
System.out.println("Пользователь " + username + " не существует.");
}
} else if( "3".equals( userInput ) ){
try {
System.out.print(" Введите имя пользователя: ");
username = in.readLine();
UserDAO userDAO = new UserDAO();
User user = userDAO.retrieveUser( username );
userDAO.deleteUser( user );
System.out.println( "Пользователь " + username + " удален из базы данных.");
} catch (Exception e) {
System.out.println("Пользователь " + username + " не существует.");
}
}
}
} catch (Exception e) {
}
}
}
package dao;
import dao.DAO;
import entity.User;
import org.hibernate.HibernateException;
import org.hibernate.Query;
/**
*
* @author seostella.com
*/
public class UserDAO extends DAO {
public User createUser(String username, String password)
throws Exception {
try {
begin();
User user = new User(username, password);
getSession().save(user);
commit();
return user;
} catch (HibernateException e) {
rollback();
throw new Exception("Could not create user " + username, e);
}
}
public User retrieveUser(String username) throws Exception {
try {
begin();
Query q = getSession().createQuery("from User where name = :username");
q.setString("username", username);
User user = (User) q.uniqueResult();
commit();
return user;
} catch (HibernateException e) {
rollback();
throw new Exception("Could not get user " + username, e);
}
}
public void deleteUser( User user ) throws Exception {
try {
begin();
getSession().delete(user);
commit();
} catch (HibernateException e) {
rollback();
throw new Exception("Could not delete user " + user.getName(), e);
}
}
}
package dao;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
/**
*
* @author seostella.com
*/
public class DAO {
private static final ThreadLocal session = new ThreadLocal();
private static final SessionFactory sessionFactory =
new AnnotationConfiguration().configure().buildSessionFactory();
protected DAO() {
}
public static Session getSession() {
Session session = (Session) DAO.session.get();
if (session == null) {
session = sessionFactory.openSession();
DAO.session.set(session);
}
return session;
}
protected void begin() {
getSession().beginTransaction();
}
protected void commit() {
getSession().getTransaction().commit();
}
protected void rollback() {
try {
getSession().getTransaction().rollback();
} catch (HibernateException e) {
}
try {
getSession().close();
} catch (HibernateException e) {
}
DAO.session.set(null);
}
public static void close() {
getSession().close();
DAO.session.set(null);
}
}
package entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author seostella.com
*/
@Entity
@Table(name="hb_user")
public class User {
private long id;
private String name;
private String password;
public User() {
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
@Column(unique=true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Id
@GeneratedValue
protected long getId() {
return id;
}
protected void setId(long id) {
this.id = id;
}
}
Caused by: org.hibernate.MappingException: Unable to load class [ entity.Article] declared in Hibernate configuration <mapping/> entry
Caused by: java.lang.ClassNotFoundException: entity.Article
<dependency>
<groupId> org.apache.cassandra</groupId>
<artifactId>cassandra-all</artifactId>
<version>0.8.1</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.13</version>
</dependency>