Reputation: 3276
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>
Upvotes: 1
Views: 1638
Reputation: 113
You can try HQL query as below
Criteria cr = session.createCriteria(Concept.class);
List results = cr.list();
Upvotes: 1
Reputation: 153690
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.
Upvotes: 4
Reputation: 1101
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
Upvotes: 0
Reputation: 1878
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.
Upvotes: 0
Reputation: 11
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!
Upvotes: 0