Java Reference
In-Depth Information
Listing 3-6. The Code to Create a Sample Message
package sample;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import sample.entity.Message;
public class PopulateMessages {
public static void main(String[] args) {
SessionFactory factory =
new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
Message m1 = new Message(
"Hibernated a message on " + new Date());
session.save(m1);
session.getTransaction().commit();
session.close();
}
}
Finally, Listing 3-7 shows the full text of the application to list all the messages in the
database.
Listing 3-7. The Message Application
package sample;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import sample.entity.Message;
Search WWH ::




Custom Search