NullPointerException While Creating Session With Hibernate 4.3.10 FINAL - java

I have created a simple Member Model Class and trying to retrieve all the members from the DB using Hibernate.
The hibernate.cfg.xml is as follows :
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://192.168.0.112:1433;DatabaseName=WBSEDCL</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">Asdf#123</property>
<property name="hibernate.connection.autocommit">true</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<!-- Mapping files -->
<mapping resource="Member.hbm.xml"/>
</session-factory>
</hibernate-configuration>
The Code For List members is as follows :
#SuppressWarnings("deprecation")
public static ArrayList<Member> listMembers(){
Session sessionNew = null;
ArrayList<Member> membrArray = new ArrayList<Member>();
try{
/* This step will read hibernate.cfg.xml and prepare hibernate for use */
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sessionFactoryNew = configuration.buildSessionFactory(ssrb.build());
sessionNew = sessionFactoryNew.openSession();
String SQL_QUERY ="SELECT * FROM Member membr";
Query query = sessionNew.createQuery(SQL_QUERY);
for(Iterator it=query.iterate();it.hasNext();){
Object[] row = (Object[]) it.next();
Member membr_obj = new Member((String)row[0],(String)row[1],(String)row[2],(String)row[3],(String)row[4],(String)row[5],(String)row[6],(String)row[7],
(String)row[8],(String)row[9],(String)row[10],(String)row[11],(String)row[12],(String)row[13],(String)row[14],(String)row[15],(String)row[16],(String)row[17],
(String)row[18],(String)row[19],(String)row[20],(String)row[21],(String)row[22],(String)row[23],(String)row[24],(String)row[25],(String)row[26],
(String)row[27],(String)row[28],(String)row[29],(String)row[30],(String)row[31],(String)row[32],(String)row[33],(String)row[34],(String)row[35],
(String)row[36],(String)row[37],(String)row[38],(String)row[39],(String)row[40],(String)row[41],(String)row[42],(String)row[43],
(String)row[44],(String)row[45],(String)row[46],(String)row[47],(String)row[48],(String)row[49],(String)row[50],(String)row[51],(String)row[52],(String)row[53]);
membrArray.add(membr_obj);
}
}
catch(Exception exception){
System.out.println("Exception in listMember Function in PersistenceManager");
exception.printStackTrace();
}
finally{
/* Actual contact insertion will happen at this step*/
sessionNew.flush();
sessionNew.close();
}
return membrArray;
}
Whenever the code is executed and the method is invoked by a calling function then a nullpointerexception is thrown at
sessionNew.flush();
sessionNew.close();
Within The finally Block.
What am I doing wrong ?

sessionNew is not initialized as you expect it to be. There seems to be an exception thrown either before its initialized or at the point of initializing it. The code should have run the catch block sysout statement after the exception is thrown. Since Finally block always run, it just gets executed calling flush() on a null object. Please check why sessionNew is not getting initialized. Try to debug and check it.

Related

"The application must supply JDBC connections" while doing simplehibernate

I am running simple hibernate program ,but it is giving me error mention in the title
My hibernate.cfg.xml is as follows:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myhiber</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">rishabh123#</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<mapping class="com.tut.ProjectwithMaven.Student"/>
</session-factory>
</hibernate-configuration>
My code is as follows:-
public class App
{
public static void main( String[] args )throws Exception
{
System.out.println( "Hello World!" );
// Configuration cfg=new Configuration();
try {
new Configuration().configure("hibernate.cfg.xml");
} catch (HibernateException e) {
// TODO Auto-generated catch block
try
{
throw (Throwable)e;
}
catch(Throwable ex)
{
ex.printStackTrace();
}
}
System.out.println("AAge");
SessionFactory factory=new Configuration().buildSessionFactory();
Student st=new Student();
st.setId(101);
st.setName("Rishabh");
st.setCity("Bangalore");
Session session=factory.openSession();
session.beginTransaction();
session.save(st);
session.getTransaction().commit();
session.close();
Please help in resolving this issue, i have checked pom.xml ,it seems to be right,but still giving same error again and again
Well, you are not using this configuration file. See the line you posted:
SessionFactory factory=new Configuration().buildSessionFactory();
It should be
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml")
.buildSessionFactory();

Getting org.hibernate.HibernateException: No CurrentSessionContext configured

I have created a maven project with hibernate 5.4 and successfully created DAOs now when I try to get Hibernate Session via getSessionFactory().getCurrentSession() I get Exception org.hibernate.HibernateException: No CurrentSessionContext configured! although I have already added
<property name="hibernate.current_session_context_class">thread</property> in hibernate.cfg.xml file. Already checked several forums including this question org.hibernate.HibernateException: No CurrentSessionContext configured but unable to solve it,
Here is the hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">onetozero</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.0.112:3306/ecdis</property>
<property name="hibernate.connection.username">root1</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="Domain.Route"/>
<mapping class="Domain.RoutePoint"/>
</session-factory>
</hibernate-configuration>
HibernateSession Class
public class HibernateSession {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
and this is how I am using getting the List via DAO
public List<T> list() {
Session session = HibernateSession.getSessionFactory().getCurrentSession();
CriteriaQuery<T> query = session.getCriteriaBuilder().createQuery(entityClass);
query.select(query.from(entityClass));
return session.createQuery(query).getResultList();
}
I am new new to Java and Hibernate, also not using any framework atm so a little detail can also go along the way. TIA
Use HibernateSession.getSessionFactory().openSession() rather than getCurrentSession() to get session, then session factory will bind session to current context as you are using current session context class thread not JTA.Details here

Hibernate - persist automatically each relation

Maybe it is a simple question, but I can't find out this situation of relations in Hibernate.
I have these Entities:
#Entity
public class User {
...
#OneToMany(mappedBy = "user")
private Set<Conversation> posts = new HashSet<Conversation>();
...
}
#Entity
public class Conversation {
...
#OneToMany(mappedBy = "conversation")
private Set<Message> messages = new HashSet<Message>();
...
}
#Entity
public class Message { ... }
and then I want create User with Conversation and Message at once. Idea should be like this:
User user = new User();
user.getPosts().add(new Conversation(){
{
getMessages().add(new Message());
}
});
session.persist(user);
But just User is saved in database - why isn't it all? Because of default LAZY fetching? Could my idea be implemented somehow?
PS: Of course I know about the solution of persisting each of the entities, but I am used to do like this in other frameworks like nette or Django, so I can't get out of my head.
PPS: I found out that problem is in default CascadeType. Could it be set on globally, e.g. in Hibernate config XML? Is it a good idea (by performance point of view - it is persisted each time on "superpersist" or only in case of changes)?
PPPS: I also found out (opposite to Django) that I have to set FK ex-post for each item added to collection. It is natural (because of selected pure Set type), but new for me. Which approach would you recommend me? Required FK as argument in constructor on item Entity e.g.:
Class Message{
Message(Conversation conversation){
setConversation(conversation);
}
...
}
or make a method for adding where FK sets inside e.g.:
Class Conversation{
...
public void addMessage(Message msg){
msg.setConversation(this);
getMessages().add(msg);
}
...
}
?
Making Session + configure XML.
private final static String CFG = "hibernate-cfg.xml";
private final static String SCRIPT_FILE = "query.sql";
private static SessionFactory sessionFactory;
private static ServiceRegistry buildRegistry() {
return new StandardServiceRegistryBuilder()
.configure(CFG)
.build();
}
private static Metadata getMetaData() {
return new MetadataSources(buildRegistry()).getMetadataBuilder().build();
}
private static SessionFactory buildSessionFactory() {
return getMetaData().getSessionFactoryBuilder().build();
}
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
sessionFactory = buildSessionFactory();
}
return sessionFactory;
}
public static Session getSession(){
try {
return getSessionFactory().openSession();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
and the hibernate-cfg.xml
<?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="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/learnme
</property>
<property name="connection.username">root</property>
<property name="connection.password"/>
<property name="connection.pool_size">100</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<!-- Display all generated SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="learnme.hibernate.entities.User"/>
<mapping class="learnme.hibernate.entities.Conversation"/>
<mapping class="learnme.hibernate.entities.Message"/>
</session-factory>
</hibernate-configuration>
According to this and this
you need to have this in your persistence.xml file to set it globally:
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" version="1.0">
<persistence-unit-metadata>
<persistence-unit-defaults>
<cascade-persist/>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
The mapping file has to be located either in the default location,
META-INF/orm.xml, or in another location that is specified explicitly
in the persistence unit definition (in persistence.xml).

Hibernate rebuilds tables of DB

I have very simple maven + hibernate project.
I want to retrieve data and print it in console with the command "select * from product". But each time i launch my app, it rebuilds all tables in db, as a result all data is removed.
What should I do so tables are not rerebuilt each time I launch my app?
Main.java
public class Main {
static final Logger logger = LogManager.getLogger(Main.class);
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
List<Object> products = null;
try {
session.beginTransaction();
SQLQuery query = session.createSQLQuery("select * from product");
query.addEntity(Product.class);
products = query.list();
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
session.close();
sessionFactory.close();
}
System.out.println("Hello");
for (Iterator iterator = products.iterator(); iterator.hasNext(); ) {
Product product = (Product) iterator.next();
logger.info("Hello");
}
}
hibernate.cfg.xml
<?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="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernate</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">****</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.jdbc.lob.non_contextual_creation">true</property>
<mapping class="models.User"/>
<mapping class="models.Role"/>
<mapping class="models.Product"/>
<mapping class="models.ProductCategory"/>
<mapping class="models.Order" />
</session-factory>
</hibernate-configuration>
Change your line
<property name="hbm2ddl.auto">create</property>
by
<property name="hbm2ddl.auto">validate</property>
Look also https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#configurations-hbmddl
Use following code in your hibernate.cfg.xml file
<property name="hbm2ddl.auto">Update</property>
instead of
<property name="hbm2ddl.auto">create</property>

Hibernate ORM without XML

I have used EclipseLink till now, but now I'm trying a Java SE project with Hibernate 4. I'm trying to do perform a NamedQuery but I get the following exception:
HibernateLog --> 18:05:03 ERROR org.hibernate.internal.SessionFactoryImpl - HHH000177: Error in named query: SubCategory.findAll
org.hibernate.hql.internal.ast.QuerySyntaxException: Subcategory is not mapped [SELECT s from Subcategory s]
This is my DBUtils class
public class DBUtils {
static final Logger logger = Logger.getLogger(DBUtils.class);
private static final ServiceRegistry serviceRegistry;
private static final SessionFactory sessionFactory;
/**
*
*/
//initialize session factory;
static {
try {
Configuration conf = new Configuration()
// mapped classes;
.addAnnotatedClass(Category.class)
.addAnnotatedClass(SubCategory.class)
.configure();
//.....
serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
sessionFactory = conf.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
logger.error("Initial SessionFactory creation failed");
System.err.println("." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static synchronized SessionFactory getSessionFactory() {
return sessionFactory;
}
}
And this is my hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<!-- Ommitted... -->
<!-- JDBC connection pool, use Hibernate internal connection pool -->
<property name="connection.pool_size">5</property>
<!-- Defines the SQL dialect used in Hibernate application -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- hibernate sql output -->
<property name="show_sql">false</property>
<property name="format_sql">false</property>
<property name="use_sql_comments">false</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.CharSet">utf8</property>
</session-factory>
</hibernate-configuration>
Finally my entities are all annotated with #Entity("tableName") annotation
What am I doing wrong?
Hibernate is case sensitive and uses the class name to map an Entity in its HQL. So
SELECT s from Subcategory s
will refer to an entity class called Subcategory. This is wrong. Use the actual class name
SELECT s from SubCategory s
as your class is named SubCategory, seen in
.addAnnotatedClass(SubCategory.class)

Categories