I downloaded Hibernate 4.1.2 and am using Oracle Database 10g Release 2. The JDBC driver I am using is ojdbc14.jar.
I set up HibernateUtil class as:
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
// Create the SessionFactory from hibernate.cfg.xml
try{
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
return configuration.buildSessionFactory(serviceRegistry);
}catch(HibernateException ex){
ex.printStackTrace();
throw ex;
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
In hibernate.properties I have:
hibernate.dialect org.hibernate.dialect.OracleDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username HR
hibernate.connection.password HR
hibernate.connection.url jdbc:oracle:thin:#localhost:1521/xe
But Hibernate doesn't want to load the driver. It throws an exception saying 'No appropriate driver found'.
I tried to load the driver with Class.forName("oracle.jdbc.driver.OracleDriver"); and it works fine.
The problem was in using the wrong JDBC Oracle driver. When I tried with ojdbc6.jar everything worked fine.
A couple of things:
Try to make the properties file valid by putting = between key and value
Check that there aren't any trailing spaces after the values
Use oracle.jdbc.OracleDriver instead of oracle.jdbc.driver.OracleDriver. See Difference between Oracle jdbc driver classes? for further reference.
Your connection URL is configured wrongly, should be:
hibernate.connection.url jdbc:oracle:thin:#localhost:1521:xe
More information for Oracle's URL can refer here.
As other answer point out:
Use oracle.jdbc.OracleDriver instead of oracle.jdbc.driver.OracleDriver
Related
I just got through the book "Just Hibernate" from O'Reilly. Some code isn't really explained fully but just given without complete description.
This code for example:
public class BasicMovieManager {
private SessionFactory sessionFactory = null;
// Creating SessionFactory using 4.2 version of Hibernate
private void initSessionFactory(){
Configuration config = new Configuration().configure();
// Build a Registry with our configuration properties
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
// create the session factory
sessionFactory = config.buildSessionFactory(serviceRegistry);
}
...
}
I just wanted to copy the code to my Hibernate-experiments, but the current stable Hibernate-version 5.2 doesn't know the class ServiceRegistryBuilder. What is a service-registry and how do I have to change the code to work with the current Hibernate-version?
The code was used to create a SessionFactory with Hibernate 4.x
The similar code for Hibernate 5.x would be something like:
StandardServiceRegistry standardRegistry =
new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata metaData =
new MetadataSources(standardRegistry).getMetadataBuilder().build();
sessionFactory = metaData.getSessionFactoryBuilder().build();
As you can see, in Hibernate 5 StandardServiceRegistry class is used. If you don't have a hibernate.cfg.xml file just use configure() method with no arguments.
See this article for further details.
I was trying to connect a simple java application with mysql db using hibernate. I have already created the schema in my db and this java application is simply creating a table in this schema and inserting data in it.All the time I am getting same error.
My Code is as follows:
hibernate.cfg.xml
UserDetails.java
HibernateTest.java
Error:
Please help me, I have been stuck for quite a long time.
Thanks !!
Seems like a duplicate of Exception in thread "main" java.util.ServiceConfigurationError
Seems that your using hibernate >=4 and the setup procedure from hibernate <4.
Correct way according to the link is.
Configuration configuration = new Configuration().configure();
ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
registry.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
//check External Jar files whether you added properly or not...if not remove all jar files and add once again...
After Main
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.OpenSession();
UserDetails ud = new UserDetails();
ud.setName("bdskbf");
ud.setId(23);
Transaction tnx = session.beginTransaction();
session.save(ud);
tnx.getTransaction.commit();
I am using a hibernate java application which uses a sessionfactory to create connections and sessions. As i am using it with a postgresql database i pass the proper jdbc connectionstring to build the sessionfactory and get my sessions from it afterwards. The only thing im able to access is the jdbc4connection.
How am i able to read which ciphersuite is used within the secured connection, which SSL protocol is used etc?
Here is how i initialize my sessionfactory:
Configuration configuration = new Configuration();
Properties p = configuration.getProperties();
p.setProperty("hibernate.connection.url","jdbc:postgresql://127.0.0.1:5432/postgres?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory");
p.setProperty("hibernate.connection.username", "myusername");
p.setProperty("hibernate.connection.password", "mypassword");
p.setProperty("hibernate.connection.driver_class",
"org.postgresql.Driver");
p.setProperty("hibernate.dialect",
"org.hibernate.dialect.PostgreSQLDialect");
ServiceRegistry serviceRegistryWebOnkys = new StandardServiceRegistryBuilder()
.applySettings(p).build();
SessionFactory sessionFactory = configuration
.buildSessionFactory(serviceRegistryWebOnkys);
If you can find out how to do it with via regular JDBC (I don't know how it's done), then you can do the same thing with Hibernate.
session.doWork(new Work() {
#Override
public void execute(Connection connection) throws SQLException {
// access the connection here and perform regular jdbc operations.
}
});
This is what I had to do when I had set some Oracle specific properties that could be set only on "OracleConnection.java" instances.
oracle.jdbc.driver.OracleConnection oc = (oracle.jdbc.driver.OracleConnection) connection.getMetaData().getConnection()
Spring application using JNDI lookup to get datasource as following:
#Bean(name = "dataSource1", destroyMethod = StringUtils.EMPTY)
public DataSource dataSource() {
final JndiDataSourceLookup lookup = new JndiDataSourceLookup();
lookup.setResourceRef(true);
return lookup.getDataSource(this.environment.getProperty(Constants.DB_JNDI_NAME_ES));
}
and getting a connection from the datasource as follows :
#Autowired
#Qualifier("dataSource1")
private DataSource ds;
Connection conn = null;
conn = this.ds.getConnection();
But when i pass that connection to StructDescriptor it throws classCastException as follows:
StructDescriptor desc1 =
StructDescriptor.createDescriptor("MSAF_DBA.AMOUNT_DUE_OBJ",conn);
java.lang.ClassCastException: weblogic.jdbc.wrapper.PoolConnection_oracle_jdbc_driver_T4CConnection cannot be cast to oracle.jdbc.OracleConnection
at oracle.sql.StructDescriptor.createDescriptor(StructDescriptor.java:101)
at oracle.sql.StructDescriptor.createDescriptor(StructDescriptor.java:72)
at com.ceiwc.es.policyholder.dao.PolicyHolderDaoImpl.getAmountDue(PolicyHolderDaoImpl.java:290)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
My understanding is the getConnection() returns a T4CConnection where as OracleConnection is required. Tried couple of ways to get the oracleConnection but cant seem to get it. Any help would be appreciated.
I believe this has to do with the contents of your war/ear file. Do not package in the Oracle driver .jar file.
Specifically, if you have ojdbc6.jar in your war file (or the equivalent) it will cause conflicts. It is fine to use that jar for compilation but you won't want it in your classpath as it is already in the Weblogic classpath by default.
See these links for similar info: here and here
I have a java application that uses hibernate for O/R mapping and I also use c3p0 connection pooling that ships together with hibernate. The DB is Oracle.
This is in my hibernate.cfg.xml:
<property name="hibernate.c3p0.min_size">3</property>
<property name="hibernate.c3p0.max_size">5</property>
<property name="hibernate.c3p0.max_statements">20</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.idleTestPeriod">120</property>
This is how I obtain the hibernate session:
public class HibernateUtil {
private static Logger log = Logger.getLogger(HibernateUtil.class);
private static final SessionFactory sessionFactory;
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure(CONFIG_FILE_LOCATION).buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
log.fatal("Initial SessionFactory creation failed." + ex.getMessage());
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
But when I run SELECT * FROM V$SESSION WHERE machine='xxx'; in Oracle the number of connections can reach 20, which is greater than max_size.
The max connections don't work in Linux environment but they do in Unix and Windows. Any setting in Linux that needs to be tweaked?
I'm also suspecting there is a cache of the application somewhere, as previously I set max_size of 20. I'm running the application in Tomcat 5.5. But I don't know of such an application cache in Tomcat.
Another info: I'm running Linux 2.6.9-34.ELsmp. Red Hat Enterprise Linux AS release 4 (Nahant Update 3)
Thanks.
I'd guess you have additional DB sessions opened not from your application. For example, there should be an additional connection - the one you're using to run the SELECT statement.