Reputation: 2992
So I'm creating a simple Hello World with hibernate with annotations.
//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>
<!-- Database connection settings -->
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<!-- <property name="connection.url">jdbc:jtds:sqlserver://127.0.0.1:1433/ProgAp3_B</property> -->
<property name="connection.url">jdbc:jtds:sqlserver://localhost:1433/ProgAp3_B</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the annotated entity class -->
<mapping class="com.Message"/>
</session-factory>
</hibernate-configuration>
Message.java
package com;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity (name="Messages")
public class Message {
@Id @GeneratedValue
private Long id;
private String text;
@ManyToOne (cascade = CascadeType.ALL)
private Message nextMessage;
public Message() { }
public Message(String text) {
this.text = text;
}
//geters and setters
}
HelloWorld.java
package com;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
public class HelloWorld {
public static void main(String[] args) {
Session session = new Configuration().configure().buildSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Message message = new Message("Hello world");
session.save(message);
tx.commit();
Transaction newTransaction = session.beginTransaction();
List<Message> messages = session.createQuery("from Message m order by m.text asc").list();
System.out.println( messages.size() + " message(s) found:" );
for (Iterator<Message> iterator = messages.iterator(); iterator.hasNext();) {
Message loaded = iterator.next();
System.out.println(loaded.getText());
}
newTransaction.commit();
session.close();
}
}
All works fine, except the second transaction.
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: Message is not mapped [from Message m order by m.text asc]
Since HQL is object oriented, I used Message because it's the class that I want to map. Why is the Message object not mapped? What is wrong with HQL?
Upvotes: 0
Views: 48
Reputation: 26067
let;s discuss about @Entity
and @Table
for example I am allowed to have same value for name attribute
@Entity(name = "someThing")
@Table(name = "someThing")
and I can have different names as well for same class
@Entity(name = "someThing")
@Table(name = "otherThing")
@Entity(name = "someThing") => this name will be used to name Entity
@Table(name = "someThing") => this name will be used to name a table in DB
So in first case your table and entity will have same name, that will allow you to access your table with same name as entity while writing HQL
or JPQL
.
And in second case while writing queries you have to use the name given in @Entity and name given in @Table will be used to name the table in DB.
so in HQL your someThing will refer to otherThing in DB
Query should be something like : from Messages m order by m.text asc
Upvotes: 1