1

So I am trying to connect to an Oracle DB, using spring and hibernate. Everything seems to work other then I can't tell I am getting a connection. In the DAOImpl class I have the following code:

    Session session = this.sessionFactory.openSession();
    System.out.println("connection: " + session.isConnected());
    System.out.println("is open: " +session.isOpen() );     
    Query q = session.createQuery("from " + Concept.class.getName());
    System.out.println(q.getQueryString());
    List<Concept> list = q.list();

The both isConnected and isOpen returns true. Yet the list is empty.

In the spring.xml file I went ahead and commented out the username and password, yet isConnected and isOpen still returns true.

So it seems that I am getting some kind of false positive on the connection. How can I check the connection, how can I know that I am connected to the right database? The database is password protected so the connection should fail.

This line:

System.out.println(q.getQueryString());

Prints:

from com.mypackage.Concept

Here is The annotated Concept:

@Entity
@Table(appliesTo = "mySchema.CONCEPT")  //I've tried with and without the schema
public class Concept {
    @Column( name="ID")
    private long id;
    @Column( name="CODE")
    private String code;
    @Column(name="description")
    private String description;

The configuration:

        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@xx1.foo.com:1521:xx" />
         <property name="username" value="user" />
        <property name="password" value="pass" />
    </bean>
    <bean id="hibernate3AnnotatedSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>mypackage.model.Concept</value>                     
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
4
  • what is this printing q.getQueryString()? Commented Mar 10, 2015 at 19:55
  • @Pete Belford, How are you confirming that data is there in the table and it's in the commit state and available in the primary cache of hibernate session? see if you have anything uncommited locally using some sql tool and you are looking at same tool with same database session which is yet to be commited. Commented Mar 18, 2015 at 13:25
  • maybe it's just a typo, but in your xml, the annotated value is mypackage.model.Concept not com.mypackage.Concept Commented Mar 23, 2015 at 16:12
  • Your @Entity doesn't appear to have an @Id. Could be causing prior errors. Please post the full Concept class. Commented Mar 23, 2015 at 16:59

5 Answers 5

4
+25

Your entity is not referenced as com.mypackage.Concept but just as Concept.

Try changing this:

Query q = session.createQuery("from " + Concept.class.getName());

to this:

Query q = session.createQuery("from " + Concept.class.getSimpleName());

or

Query q = session.createQuery("from Concept");

If this still doesn't return any result, you can run a native query to check the available data:

String sql = "SELECT * FROM mySchema.CONCEPT";
SQLQuery query = session.createSQLQuery(sql);
List<Object[]> entities = query.list();

If this doesn't return any data then you don't have data on this table.

Sign up to request clarification or add additional context in comments.

2 Comments

I've tried a couple of different things...using the createSqlQuery and getSimpleName. Now I get an error that Concept is not mapped.
Try without a schema: "SELECT * FROM CONCEPT";
1

You can try HQL query as below

Criteria cr = session.createCriteria(Concept.class);
List results = cr.list();

Comments

0

Remember that when using HQL you only have to use the class name. Therefore, you query should look like this:

Query q = session.createQuery("from Concept");

Hibernate has two options when building queries, one is using HQL and the second one is using plain SQL, for the second option you should use createSQLQuery.

Good luck coding!

Comments

0

Please make sure that "annotatedClass" property of hibernate configuratation should contain the FullyQualified name for the Class. And Same thing to be followed while quering to the Database using HQL.

I can see,

You are querying like "FROM com.mypackage.Concept". But, If we see the Hibernate Configuration for "annotatedClass" property , You have written "mypackage.model.Concept" .

It should give some exception . If You have posted the exact programme.

Comments

0

I dont know exactly your context here, just one case for you to check, In your AnnotationSessionFactoryBean bean, change

<property name="annotatedClasses">
   <list>
       <value>mypackage.model.Concept</value>                     
   </list>
</property>

Into

<property name="packagesToScan" value="your_package_where_your_entities_can_be_found" />

To make hibernate able to understand your entities

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.