Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within blogs
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Blog around the Campfire
Simple Hibernate Example with HashMap
Christopher Rygaard
Ranch Hand
Posts: 42
3
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Simple HashMap Example
This is the simplest stand-alone Hibernate HashMap example I could cook up.
Notice: This is provided with no warranty whatsoever. Use at your own risk.
This is the contained class:
@javax.persistence.Entity class Contained { @javax.persistence.EmbeddedId private String _name; private String _containedData; private Contained() { } public Contained( String name, String data ) { _name = name; _containedData = data; } public String getName() { return _name; } public void display() { System.out.println( " Contained: data = " + _containedData ); } }
This is the containing class:
@javax.persistence.Entity class Container { @javax.persistence.OneToMany() private java.util.Map<String,Contained> _hashMap = new java.util.HashMap<String,Contained>(); @javax.persistence.Id @javax.persistence.GeneratedValue( generator = "increment" ) @org.hibernate.annotations.GenericGenerator( name = "increment", strategy = "increment" ) private long _containerID; private String _containerData; private Container() { } public Container( String data ) { _containerData = data; } public void addContained( String name, Contained contained ) { _hashMap.put( name, contained ); } public void display() { System.out.println( "Container: data = " + _containerData ); for ( Contained contained : _hashMap.values() ) { contained.display(); } } }
This is an example of how to use it:
public class HibernateExample { private org.hibernate.SessionFactory _sessionFactory; final static public void main( String arguments[] ) { HibernateExample example; long indexOffset; example = new HibernateExample(); example.makeContents(); example.showContents(); example.cleanup(); } private HibernateExample() { org.hibernate.cfg.Configuration configuration; java.util.logging.Logger.getLogger( "org.hibernate" ).setLevel( java.util.logging.Level.SEVERE ); // Supress Hibernate's excessive output configuration = new org.hibernate.cfg.Configuration(); configuration.setProperty( "hibernate.dialect", "org.hibernate.dialect.H2Dialect" ); // Customize this for your particular RDBMS configuration.setProperty( "hibernate.connection.driver_class", "org.h2.Driver" ); // Customize this for your particular RDBMS configuration.setProperty( "hibernate.connection.url", "jdbc:h2:file:C:/SFTData/SFTDatabases/SFTInterfaceData;DB_CLOSE_DELAY=-1" ); // Customize this for your particular RDBMS configuration.setProperty( "hibernate.connection.username", "sa" ); // Customize this for your particular RDBMS configuration.setProperty( "hibernate.connection.password", "" ); // Customize this for your particular RDBMS installation configuration.setProperty( "hibernate.connection.pool_size", "1" ); // Customize this for your particular RDBMS installation configuration.setProperty( "hibernate.cache.provider_class", "org.hibernate.cache.internal.NoCacheProvider" ); // This is not ready for prime-time configuration.setProperty( "hibernate.show_sql", "false" ); // Tell hibernate to not echo the SQL configuration.setProperty( "hibernate.hbm2ddl.auto", "create" ); configuration.addAnnotatedClass( Container.class ); configuration.addAnnotatedClass( Contained.class ); _sessionFactory = configuration.buildSessionFactory(); } final private void makeContents() { org.hibernate.Session session; boolean committed; org.hibernate.Transaction transaction; Container container1; Contained contained11; Contained contained12; Container container2; Contained contained21; Contained contained22; Contained contained23; session = _sessionFactory.openSession(); try { committed = false; transaction = session.beginTransaction(); try { container1 = new Container( "container1" ); contained11 = new Contained( "contained11Name", "contained11Data" ); container1.addContained( contained11.getName(), contained11 ); contained12 = new Contained( "contained12Name", "contained12Data" ); container1.addContained( contained12.getName(), contained12 ); session.save( contained11 ); session.save( contained12 ); session.save( container1 ); container2 = new Container( "container2" ); contained21 = new Contained( "contained21Name", "contained21Data" ); container2.addContained( contained21.getName(), contained21 ); contained22 = new Contained( "contained22Name", "contained22Data" ); container2.addContained( contained22.getName(), contained22 ); contained23 = new Contained( "contained23Name", "contained23Data" ); container2.addContained( contained23.getName(), contained23 ); session.save( contained21 ); session.save( contained22 ); session.save( contained23 ); session.save( container2 ); transaction.commit(); committed = true; } finally { if ( !committed ) { transaction.rollback(); } } } finally { session.close(); } return; } final private void showContents() { org.hibernate.Session session; boolean committed; org.hibernate.Transaction transaction; java.util.List<Container> containers; session = _sessionFactory.openSession(); try { committed = false; transaction = session.beginTransaction(); try { containers = (java.util.List<Container>)( session.createQuery("from Container").list() ); System.out.println( "List:" ); for ( Container container : containers ) { container.display(); } transaction.commit(); committed = true; } finally { if ( !committed ) { transaction.rollback(); } } } finally { session.close(); } return; } final private void cleanup() { if ( _sessionFactory != null ) { _sessionFactory.close(); } } }
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Simple Hibernate Container Example
Simple Hibernate Example with Final Classes and Final Methods
Simple Hibernate Cascade Save Example
Simple Hibernate Lazy Loading Example
Simple Hibernate Eager Loading Example
More...