How to use getCurrentSession in Hibernate? - java

Configuration hibernate
Properties prop= new Properties();
prop.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
prop.setProperty("hibernate.connection.url", url);
prop.setProperty("hibernate.connection.username", user);
prop.setProperty("hibernate.connection.password", password);
concreteSessionFactory = new AnnotationConfiguration()
.addPackage("Main.*")
.addProperties(prop)
.addAnnotatedClass(DeviceDataSet.class)
.buildSessionFactory();
public SessionFactory sessionFactory() {
return concreteSessionFactory;
}
I use
public long insertNewJournalNote(JournalDataSet journalDataSet) {
Session session = sessionFactory.openSession();
MySqlDAO dao = new MySqlDAO(session);
return dao.insertNewJournalNote(journalDataSet);
}
With getCurrentSession, I get:
Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured

Maybe this line could solve your problem:
prop.setProperty("hibernate.current_session_context_class", thread);

Related

How to skip "information_schema.sequences" creation while using Hibernate in Java projects

I am using Redshift as a DB for my Java Project with Hibernate support. When the Hibernate starts, it tries to create a sequence, which I do not need.
public class HibernateUtil {
private static SessionFactory SESSION_FACTORY;
private static StandardServiceRegistry registry;
public static synchronized SessionFactory getSessionFactory(DataSource dataSource) {
if (SESSION_FACTORY == null) {
try {
Configuration configuration = new Configuration();
Properties settings = new Properties();
settings.put("show_sql", "false");
settings.put("current_session_context_class", "thread");
settings.put("hbm2ddl.auto", "none");
configuration.setProperties(settings);
registry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.applySetting(Environment.DATASOURCE, dataSource)
.build();
SESSION_FACTORY = configuration.buildSessionFactory(registry);
} catch (Exception e) {
log.error("Error creating Session Factory.", e);
if (registry != null) {
StandardServiceRegistryBuilder.destroy(registry);
}
}
}
return SESSION_FACTORY; }
}
This is the stack trace.
ERROR [2019-06-07 14:41:40,743] org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl: Could not fetch the SequenceInformation from the database
! com.amazon.support.exceptions.ErrorException: [Amazon](500310) Invalid operation: relation "information_schema.sequences" does not exist;
! ... 52 common frames omitted
! Causing: java.sql.SQLException: [Amazon](500310) Invalid operation: relation "information_schema.sequences" does not exist;
I created a custom Dialect that overrode existing PostgreSqlDialect
public class CustomRedshiftDialect extends PostgreSQL81Dialect {
#Override
public String getQuerySequencesString() {
return null;
}
}
And in the HibernateUtil class or the appropriate XML.
// Hibernate settings equivalent to hibernate.cfg.xml's properties
Properties settings = new Properties();
settings.put("show_sql", "false");
settings.put("current_session_context_class", "thread");
settings.put("hbm2ddl.auto", "none");
settings.put("hibernate.dialect", "com.me.vlimbare.factory.db.CustomRedshiftDialect");

Nullpointer exception while connecting to DB using MyBatis

I am having the following issue: when I am connecting to db using MyBatis I get an NPE.
Here is the class for setting connection:
public class SetDBConnection {
private Mapper mapper;
private SqlSession session;
private Configuration configuration;
public SetDBConnection() {
configuration = new Configuration();
configuration.setJdbcTypeForNull(JdbcType.VARCHAR);
configuration.setLogImpl(Slf4jImpl.class);
configuration.setDefaultExecutorType(ExecutorType.REUSE);
configuration.addMapper(Mapper.class);
Properties properties = null;
try {
properties = readProperties();
} catch (IOException e) {
e.printStackTrace();
}
configuration.setVariables(properties);
}
public Mapper openSession() {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
session = sqlSessionFactory.openSession();
mapper = session.getMapper(Mapper.class);
return mapper;
}
public void closeSession() {
session.commit();
session.close();
}
private Properties readProperties() throws IOException {
Properties properties = new Properties();
InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream("connection.properties");
if (inputStream != null) {
properties.load(inputStream);
} else {
throw new FileNotFoundException("property file not found in the classpath");
}
inputStream.close();
return properties;
}
}
And here I call it and try to Insert data
SetDBConnection conn = new SetDBConnection();
Person p = new Person();
p.setName("Alex");
p.setLastName("Bondar");
p.setTelephone("+79267157256");
p.setPersonalId("777-216");
Mapper mapper=conn.openSession();
try {
System.out.println("All set to go");
mapper.saveOrUpdatePerson(p);
} finally {
conn.closeSession();
}
Stacktrace is the following:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error opening session. Cause: java.lang.NullPointerException
### The error may exist in org/abondar/experimental/mybatisdemo/mappers/Mapper.java (best guess)
### Cause: java.lang.NullPointerException
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:100)
at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:47)
at org.abondar.experimental.mybatisdemo.SetDBConnection.openSession(SetDBConnection.java:51)
at org.abondar.experimental.mybatisdemo.Main.main(Main.java:19)
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:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.NullPointerException
at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:95)
... 8 more
What can be wrong and is there any way not to re-establish DB-connection for every action(select,insert or delete)?
It seems you have no Environment and TransactionFactory defined. According to the docs you should initialize MyBatis somehow like this:
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
Thanks Frank for DataSource idea. I used default pooled dataSource and this problem looks to be solved

java.lang.NoSuchFieldError: namingStrategy

There's an example from the web on how to use annotations in Hibernate (before that I've worked on the same example, but it used .xml instead. And I've managed to make it work without exceptions).
So now I have:
Initial session factory creation failedjava.lang.NoSuchFieldError: namingStrategy
Exception in thread "main" java.lang.ExceptionInInitializerError
at firstproject.HibernateUtil.<clinit>(HibernateUtil.java:14)
at firstproject.StudentDAO.addSubject(StudentDAO.java:82)
at firstproject.Test.main(Test.java:12) Caused by: java.lang.NoSuchFieldError: namingStrategy
at org.hibernate.cfg.AnnotationConfiguration.reset(AnnotationConfiguration.java:250)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:125)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:119)
at org.hibernate.cfg.AnnotationConfiguration.<init>(AnnotationConfiguration.java:108)
at firstproject.HibernateUtil.<clinit>(HibernateUtil.java:11)
... 2 more
Here is some code, that may help:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); // HibernateUtil.java:11
} catch (Throwable ex) {
System.err.println("Initial session factory creation failed" + ex);
throw new ExceptionInInitializerError(ex); // HibernateUtil.java:14
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
public class StudentDAO {
public Long addSubject(Subject subject) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession(); // StudentDAO.java:82
session.beginTransaction();
Long result = (Long) session.save(subject);
session.getTransaction().commit();
return result;
}
}
public class Test {
public static void main(String[] args) {
StudentDAO dao = new StudentDAO();
Subject subject = new Subject();
subject.setSubjectName("Mathematics");
dao.addSubject(subject); // Test.java:12
}
}
Hi Kleeo
You have written the following line in HibernateUtil class.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Replace this line of code with the below written line & retry. I hope this will work for you.
sessionFactory = new Configuration().configure().buildSessionFactory();
AnnotationConfiguration has been Deprecated in Hibernate 3.6.
As you can see in the documentation (see link below) all functionality has been moved to Configuration.
You can use safely Configuration instead.
sessionFactory = new Configuration().configure().buildSessionFactory();
AnnotationConfiguration documentation:
http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/cfg/AnnotationConfiguration.html
Use below :
sessionFactory = new Configuration().configure().buildSessionFactory();
And also ensure that your cfg.xml should be present in root of src folder. Else you will get exception of unable to find cfg file

Hibernate package scan in JavaConfig

I'm trying to configure Spring and Hibernate without xml. Here's my SessionFactory bean. When I add annotated class to configuration - it works properly. I want to do it automatically, but adding a package to configuration doesnt helps for some reason, I get "Identificator is not mapped" error
#Bean
public SessionFactory sessionFactory(){
Properties hibernateProperties = new Properties();
hibernateProperties.put("hibernate.connection.driver_class",ds_driver);
hibernateProperties.put("hibernate.connection.url",ds_url);
hibernateProperties.put("hibernate.connection.username",ds_username);
hibernateProperties.put("hibernate.connection.password",ds_password);
hibernateProperties.put("hibernate.show_sql", false);
hibernateProperties.put("connection.pool_size", 1);
hibernateProperties.put("current_session_context_class", "thread");
hibernateProperties.put("hibernate.hbm2ddl.auto", "update");
org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration();
configuration.addPackage("app.entity"); // **doesnt work**
configuration.addAnnotatedClass(Identificator.class); // **works fine**
configuration.addProperties(hibernateProperties);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
The #addPackage() method reads the package-level metadata, and not the classes in the package. Unfortunately the Configuration class does not provide methods the achive what you want, you have to pass all classes to #addAnnotatedClass().
A possible (but not sure if recommendable) solution would be using another solution to find the required class descriptors, build a list from them, and then pass them in a loop to #addAnnotatedClass(). I am quite sure, that Spring has solutions for this.
public SessionFactory getSessionFactory() {
if (sessionFactory == null) {
Configuration configuration = new Configuration()
.configure(getMappedValue("Universal", "qb_hibernate"))
.setProperty("hibernate.connection.autocommit", "true")
.setProperty("connection.pool_size", "20000")
.setProperty("hibernate.dialect", getMappedValue("Universal", "dialect"))
.setProperty("hibernate.connection.driver_class", getMappedValue("Universal", "driver_class"))
.setProperty("hibernate.connection.url", getMappedValue("Universal", "url"))
.setProperty("hibernate.connection.username", getMappedValue("Universal", "userName"))
.setProperty("hibernate.connection.password", getMappedValue("Universal", "password"))
.setProperty("hibernate.show_sql", "false")
.setProperty("hibernate.current_session_context_class", "thread")
.setProperty("hibernate.query.factory_class", "org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory");
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
LocalSessionFactoryBuilder builder
= new LocalSessionFactoryBuilder(dataSource());
builder.scanPackages("zw.co.techno.xxxxx.model").buildSettings(serviceRegistry);
// builds a session factory from the service registry
// sessionFactory = configuration.buildSessionFactory(serviceRegistry);
sessionFactory = builder.buildSessionFactory();
}
return sessionFactory;
}

hibernate-can't run " cfg.configure()" outside of method?

due to the frequently insert event , (it's a chat room program)
it's always occurs "exception : java heap outOfMemory...."
and i think that it's cause from here
here is my correct code
public static boolean saveMethod(String userid,String username,String msg){
Configuration cfg = new Configuration();
cfg.configure(); //here is correct
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction tx = null;
tx = session.beginTransaction();
Transaction transaction = null;
try{
....jump
transaction = session.beginTransaction();
session.save(saveMsg);
transaction.commit();
return true;
}catch(Exception e){
return false;
}
}
so i change the method to that
Configuration cfg = new Configuration();
cfg.configure();// error here
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction tx = null;
tx = session.beginTransaction();
Transaction transaction = null;
public static boolean setPublicMsg(String userid,String username,String msg){
//insert
}
Syntax error on token "configure", Identifier expected after this
token
how can i write here ?
or anyone have some idea about the "exception : java heap outOfMemory...." in hibernate?
i'm try to change the tomcat setting, but it's not working
thanks !
Just by looking at your saveMethod(), it looks like you are creating configuration and sessionFactory per each save call. You can simply avoid this.
When your application starts, you can build the sessionFactory once and reuse as much as you need.
For example, you can use a HibernateUtil class to get the sessionFactory as below.
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null ) {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
return sessionFactory;
}
}
Then in your saveMethod() you get the sessionFactory as below.
public static boolean saveMethod(String userid,String username,String msg){
SessionFactory factory = HibernateUtil.getSessionFactory();
// ...
}

Categories