I want to use HibernateOGM to interact with MongoDB. I have an cfg.xml file like this:
<hibernate-configuration>
<session-factory>
<property name="hibernate.transaction.factory_class">org.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.ogm.datastore.provider">mongodb</property>
<property name="hibernate.ogm.datastore.grid_dialect">org.hibernate.ogm.dialect.MongoDBDialect</property>
<property name="hibernate.ogm.mongodb.database">rcfdb</property>
<property name="hibernate.ogm.mongodb.host">127.0.0.1</property>
<property name="hibernate.ogm.mongodb.port">27017</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<mapping resource="hibernate-contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I have also wrote my POJO class, and in the main class I want to populate the database in the mongodb with this code, but I am not able to do this job and I get these line of Infos, how can I solve that:
Session session=null;
OgmConfiguration cfgogm=new OgmConfiguration();
SessionFactory sessionfactory= cfgogm.buildSessionFactory();
session=sessionfactory.openSession();
session.beginTransaction();
System.out.println("Populating the database...");
Contact cnt=new Contact();
cnt.setFirstname("Blabla");
cnt.setLastname("Blabla");
cnt.setEmail("blabla");
session.save(cnt);
session.getTransaction().commit();
System.out.println("Done... :)");
I have no output with this code, and also no exceptions
INFO Lines:
This is the structure of my project:
You have specified MongoDBDialect in the configuration file. But on the console log, you are getting NoopDialect on HHH000400.
And in the next line, you are getting connection was null.
And the last line is, Unable to create requested service.
hibernate.ogm.mongodb.port -> hibernate.ogm.datastore.port
I puzzled over this for two days
Related
I am having a difficult time trying to set a connection with hibernate to a SQL Server. I should create a connection to the following data source DataSource=server,port. It seem strange that the port must be specify with a comma instead of :
When I connect to it through Visual Studio 2012 this is how it looks:
Data Source=server,17001;Initial Catalog=database;User ID=username
In order to connect it I create the following hibernate configuration file:
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://server.fqdn</property>
<property name="hibernate.connection.port">port</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.databasename">database</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.enable_lazy_load_no_trans">true</property>
<property name="connection.pool_size">1000</property>
</session-factory>
</hibernate-configuration>
But I get the following Exception when the connection is attempted:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'username'. ClientConnectionId:c619c1dc-2c6c-4c6e-bc81-7d3619ee9ff1
I know for sure that the user and password are correct as I am using them to connect to the database through Visual Studio 2012 and they works fine.
Any idea of how should I face it?
I forgot to mark this as resolved. The issue was that I was specifying the port as a separate property:
<property name="hibernate.connection.port">port</property>.
When I add it to the connection string parameter
<property name="hibernate.connection.url">jdbc:sqlserver://server.fqdn:port</property>
it works
Hi I am implementing hibernate, When I am trying to insert a new record getting the Exception org.hibernate.exception.sqlgrammarexception
My hibernate.cfg.xml file :
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_db</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">system</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.hib.UserDetails"/>
</session-factory>
</hibernate-configuration>
If I changed the
<property name="hbm2ddl.auto">update</property>
<mapping class="com.hib.UserDetails"/>
It worked, But I have to create table manually.
What might be the issue?
Look for the piece of code where you are throwing your SQL/HQL query.
There you may find some simple yet hard to find mistakes like wrongly spelt commands,etc.
That's why sql 'grammar' exception
Check mapping class "UserDeatails".
There is mismatch between manually created table and UserDetails mapping class.
Possibly mismatch Column_Names, Id, Constraints ..etc
check for column names in table and match with column names in #column annotation in bean. Most probably its column mismatch.
Could you post the Stacktrace of the Exception, also it would be of help if you could post the code which is adding the User to Database.
It can either be a problem with the Column names of Database table and the declared variables.
<property name="show_sql">true</property>
<property name="format_sql">true</property>
These must be generating the SQL Statement, check if there is an error in the Generated SQL Statement. Posting the SQL could also help.
i develop a J2EE application with Hibernate, and i want to have 2 database, the first will be local (in client computer ), and the second will be in the server.
indeed , i want to configure hibernate to connect in the local databases when the global is not accessible.
My actual config file :
<hibernate-configuration >
<session-factory>
<property name= "hibernate.dialect"> org.hibernate.dialect.MySQLDialect</property>
<property name= "hibernate.connection.driver_class"> com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/serverBdd </property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="hibernate.connection.password"></property>
<mapping resource="hibernateConfiguration/Usine.hbm.xml"/>
<mapping resource="hibernateConfiguration/Produit.hbm.xml"/>
<mapping resource="hibernateConfiguration/Machine.hbm.xml"/>
<mapping resource="hibernateConfiguration/Operation.hbm.xml"/>
<mapping resource="hibernateConfiguration/Utilisateur.hbm.xml"/>
</session-factory>
</hibernate-configuration>
how Can i do this ? and if i can't what should i do to solve my problem ?
thanks
Sure, here you can find an example that writes in 2 databases at same time, but with a simple if condition you can decide which to use:
First, create 2 hibernate.cfg.xml files as usual but with different names
serverconfig.cfg.xml
localconfig.cfg.xml
After, in your DAO or wherever you get your hibernate Session simply check if server database and if it is not available use local configuration:
SessionFactory sessionFactory = new Configuration().configure("serverconfig.cfg.xml").buildSessionFactory();
if (sessionFactory == null)
sessionFactory = new Configuration().configure("localconfig.cfg.xml").buildSessionFactory();
Also, if you use Hibernate 4 or later check this answer
i am trying Hibernate 4 in Netbeans 8, the problem is that after the the committed is done the application still running.
Here is the code
public class TestHibernateAnotation {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Users user = new Users();
Users user2 = new Users();
user2.setUser_name("Djalil");
user.setUser_name("Daniel");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(user);
session.save(user2);
session.getTransaction().commit();
session.close();
System.out.print("End of code");
}
}
My Hibernate Config
<?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.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/SalesTest</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="testhibernateanotation.NewClass"/>
<mapping class="testhibernateanotation.Users"/>
any Idea about this issue
thank you,
You need to explicitly destroy the service registry after session.close();. This seems to be a bug in newer hibernate versions. So basically your code that cleans up at the end should look like :
sessionFactory.close();
StandardServiceRegistryBuilder.destroy(sessionFactory.getSessionFactoryOptions().getServiceRegistry());
Plug: I had posted an example here - including this issue.
I found the solution (in an other post),
it would be adding this to your config files:
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.timeout">100</property>
They said that it is bug in hibernate 4
you will have to close the SessionFactory object [ HibernateUtil.getSessionFactory().openSession() ]
The SessionFactory is a heavyweight object; it is usually created during application start up and kept for later use.The SessionFactory is a thread safe object and used by all the threads of an application.
We can create one SessionFactory implementation per database in any application. If your application is referring to multiple databases, then you need to create one SessionFactory per database.
its how hibernate SessionFactory was designed.
https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/SessionFactory.html
I have a project using JBoss + hibernate, and NOT JPA (don't ask me why, just assume this way). So we're using hibernate.cfg.xml and hibernate Session. Well, I'm trying to make JBoss to handle my session, so I changed my hibernate.cfg.xml to something like this:
<hibernate-configuration>
<session-factory name="unitName">
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.connection.datasource">MyDS</property>
</session-factory>
</hibernate-configuration>
And in my code I just added:
#PersistenceContext(unitName="unitName") protected Session session;
But when I run the application, it goes:
Caused by: java.lang.IllegalArgumentException: Can't find a persistence unit named 'unitName' in ...
Tried changing from hibernate.cfg.xml to persistence.xml, but still using Session, and I got an error similar to this: https://issues.jboss.org/browse/JBAS-8815
I know, there is a workaround for this error, but my main question is: Can I use a managed Datasource with plain Hibernate/Session and hibernate.cfg.xml? What I'm doing wrong?
Thanks!