Hibernate notes

「Hibernate notes」の編集履歴(バックアップ)一覧はこちら

Hibernate notes」(2006/03/20 (月) 10:49:11) の最新版変更点

追加された行は緑色になります。

削除された行は赤色になります。

* Hibernate intro notes ** Create POJO class package hello; public class Message { private Long id; private String text; private Message nextMessage; private Message() {} public Message(String text) { this.text = text; } public Long getId() { return id; } private void setId(Long id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public Message getNextMessage() { return nextMessage; } public void setNextMessage(Message nextMessage) { this.nextMessage = nextMessage; } } ** Simple Hibernate operations package hello; import java.util.*; //import org.hibernate.classic.*; import org.hibernate.classic.Session; import org.hibernate.cfg.*; import org.hibernate.Transaction; import org.hibernate.SessionFactory; public class main { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.setProperties(System.getProperties()); cfg.configure(); // cfg.addResource("hello/Message.hbm.xml"); // cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); SessionFactory sessions = cfg.buildSessionFactory(); //Session session = getSessionFactory().openSession(); Session session = sessions.openSession(); Transaction tx = session.beginTransaction(); Message message = new Message("Hello World"); session.save(message); //--- message = (Message)session.load(Message.class, new Long(1)); message.setText("Greetings Earthling"); Message nextMessage = new Message("Take me to your leader (please)"); message.setNextMessage(nextMessage); //--- tx.commit(); session.close(); // Session newSession = getSessionFactory().openSession(); Session newSession = sessions.openSession(); Transaction newTransaction = newSession.beginTransaction(); List messages = newSession.find("from Message as m order by m.text asc"); // List messages = newSession.createQuery("from Message as m order by m.text asc").list(); System.out.println(messages.size() + " message(s) found:"); for (Iterator iter = messages.iterator(); iter.hasNext(); ) { message = (Message)iter.next(); System.out.println(message.getText()); } newTransaction.commit(); newSession.close(); } } ** Top level configuration files in unmanaged environment Properties can be specified in hibernate.properties or hibernate.cfg.xml. The latter has higher precedence. hibernate.properties: hibernate.connection.driver_class com.mysql.jdbc.Driver hibernate.connection.url jdbc:mysql://localhost/sampledbname hibernate.connection.username user hibernate.connection.password password hibernate.dialect org.hibernate.dialect.MySQLDialect # hibernate.c3p0.min_size 5 # hibernate.c3p0.max_size 20 # hibernate.c3p0.timeout 300 # hibernate.c3p0.max_statements 50 # hibernate.c3p0.idle_test_period 3000 hibernate.connection.pool_size 1 hibernate.c3p0.max_size 2 hibernate.c3p0.min_size 2 hibernate.c3p0.timeout 5000 hibernate.c3p0.max_statements 100 hibernate.c3p0.idle_test_period 3000 hibernate.c3p0.acquire_increment 2 hibernate.c3p0.validate false hibernate.cfg.xml <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/sampledb</property> <property name="connection.username">user</property> <property name="connection.password">password</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.pool_size">1</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <property name="c3p0.min_size">5</property> <property name="c3p0.max_size">20</property> <property name="c3p0.timeout">300</property> <property name="c3p0.max_statements">50</property> <property name="c3p0.idle_test_period">300</property> <property name="hbm2ddl.auto">create</property> <mapping resource="hello/Message.hbm.xml"/> </session-factory> <!-- <session-factory name="foo"> <property name="show_sql">true</property> <mapping resource="org/hibernate/test/legacy/Simple.hbm.xml"/> <class-cache class="org.hibernate.test.legacy.Simple" region="Simple" usage="read-write"/> </session-factory> --> </hibernate-configuration> ** Config file for persistent class hello/Message.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="hello.Message" table="MESSAGES"> <id name="id" column="MESSAGE_ID"> <generator class="increment"/> </id> <property name="text" column="MESSAGE_TEXT"/> <many-to-one name="nextMessage" cascade="all" column="NEXT_MESSAGE_ID"/> </class> </hibernate-mapping> ** log4j.properties ### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### root logger option ### log4j.rootLogger=warn, stdout ### Hibernate logging options ### log4j.logger.org.hibernate=warn ** build.xml <project name="hibernate_hello" default="compile" basedir="."> <description> Hibernate hello world </description> <property name="src" location="."/> <property name="build" location="."/> <property name="hibernate.jar" location="c:/usr/local/hibernate-3.1/hibernate3.jar"/> <property name="mysqljdbc.jar" location="c:/usr/local/mysql-connector-java-3.1.12/mysql-connector-java-3.1.12-bin.jar"/> <property name="dom4j.jar" location="c:/usr/local/lib/dom4j-1.6.1.jar"/> <property name="commons-logging.jar" location="c:/usr/local/lib/commons-logging.jar"/> <property name="commons-collections.jar" location="c:/usr/local/lib/commons-collections-3.1.jar"/> <property name="log4j.jar" location="c:/usr/local/lib/log4j-1.2.9.jar"/> <property name="hibernate.lib" location="c:/usr/local/hibernate-3.1"/> <target name="compile"> <javac srcdir="${src}" destdir="${build}" classpath="${hibernate.jar}" debug="true" > </javac> </target> <target name="run" depends="compile"> <java classname="hello.main" fork="true"> <classpath> <pathelement location="./"/> <pathelement location="${hibernate.jar}"/> <pathelement location="${mysqljdbc.jar}"/> <pathelement location="${dom4j.jar}"/> <pathelement location="${commons-logging.jar}"/> <pathelement location="${commons-collections.jar}"/> <pathelement location="${log4j.jar}"/> <pathelement location="c:/home/toshima/hibernate"/> <fileset dir="${hibernate.lib}"> <include name="**/*.jar"/> </fileset> </classpath> </java> </target> </project>
* Hibernate intro notes ** Create POJO class package hello; public class Message { private Long id; private String text; private Message nextMessage; private Message() {} public Message(String text) { this.text = text; } public Long getId() { return id; } private void setId(Long id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public Message getNextMessage() { return nextMessage; } public void setNextMessage(Message nextMessage) { this.nextMessage = nextMessage; } } ** Simple Hibernate operations package hello; import java.util.*; //import org.hibernate.classic.*; import org.hibernate.classic.Session; import org.hibernate.cfg.*; import org.hibernate.Transaction; import org.hibernate.SessionFactory; public class main { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.setProperties(System.getProperties()); cfg.configure(); // cfg.addResource("hello/Message.hbm.xml"); // cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); SessionFactory sessions = cfg.buildSessionFactory(); //Session session = getSessionFactory().openSession(); Session session = sessions.openSession(); Transaction tx = session.beginTransaction(); Message message = new Message("Hello World"); session.save(message); //--- message = (Message)session.load(Message.class, new Long(1)); message.setText("Greetings Earthling"); Message nextMessage = new Message("Take me to your leader (please)"); message.setNextMessage(nextMessage); //--- tx.commit(); session.close(); // Session newSession = getSessionFactory().openSession(); Session newSession = sessions.openSession(); Transaction newTransaction = newSession.beginTransaction(); List messages = newSession.find("from Message as m order by m.text asc"); // List messages = newSession.createQuery("from Message as m order by m.text asc").list(); System.out.println(messages.size() + " message(s) found:"); for (Iterator iter = messages.iterator(); iter.hasNext(); ) { message = (Message)iter.next(); System.out.println(message.getText()); } newTransaction.commit(); newSession.close(); } } ** Top level configuration files in unmanaged environment Properties can be specified in hibernate.properties or hibernate.cfg.xml. The latter has higher precedence. hibernate.properties: hibernate.connection.driver_class com.mysql.jdbc.Driver hibernate.connection.url jdbc:mysql://localhost/sampledbname hibernate.connection.username user hibernate.connection.password password hibernate.dialect org.hibernate.dialect.MySQLDialect # hibernate.c3p0.min_size 5 # hibernate.c3p0.max_size 20 # hibernate.c3p0.timeout 300 # hibernate.c3p0.max_statements 50 # hibernate.c3p0.idle_test_period 3000 hibernate.connection.pool_size 1 hibernate.c3p0.max_size 2 hibernate.c3p0.min_size 2 hibernate.c3p0.timeout 5000 hibernate.c3p0.max_statements 100 hibernate.c3p0.idle_test_period 3000 hibernate.c3p0.acquire_increment 2 hibernate.c3p0.validate false hibernate.cfg.xml <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/sampledb</property> <property name="connection.username">user</property> <property name="connection.password">password</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.pool_size">1</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <property name="c3p0.min_size">5</property> <property name="c3p0.max_size">20</property> <property name="c3p0.timeout">300</property> <property name="c3p0.max_statements">50</property> <property name="c3p0.idle_test_period">300</property> <property name="hbm2ddl.auto">create</property> <mapping resource="hello/Message.hbm.xml"/> </session-factory> <!-- <session-factory name="foo"> <property name="show_sql">true</property> <mapping resource="org/hibernate/test/legacy/Simple.hbm.xml"/> <class-cache class="org.hibernate.test.legacy.Simple" region="Simple" usage="read-write"/> </session-factory> --> </hibernate-configuration> ** Config file for persistent class hello/Message.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="hello.Message" table="MESSAGES"> <id name="id" column="MESSAGE_ID"> <generator class="increment"/> </id> <property name="text" column="MESSAGE_TEXT"/> <many-to-one name="nextMessage" cascade="all" column="NEXT_MESSAGE_ID"/> </class> </hibernate-mapping> ** log4j.properties ### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### root logger option ### log4j.rootLogger=warn, stdout ### Hibernate logging options ### log4j.logger.org.hibernate=warn ** build.xml <project name="hibernate_hello" default="compile" basedir="."> <description> Hibernate hello world </description> <property name="src" location="."/> <property name="build" location="."/> <property name="hibernate.jar" location="c:/usr/local/hibernate-3.1/hibernate3.jar"/> <property name="mysqljdbc.jar" location="c:/usr/local/mysql-connector-java-3.1.12/mysql-connector-java-3.1.12-bin.jar"/> <property name="dom4j.jar" location="c:/usr/local/lib/dom4j-1.6.1.jar"/> <property name="commons-logging.jar" location="c:/usr/local/lib/commons-logging.jar"/> <property name="commons-collections.jar" location="c:/usr/local/lib/commons-collections-3.1.jar"/> <property name="log4j.jar" location="c:/usr/local/lib/log4j-1.2.9.jar"/> <property name="hibernate.lib" location="c:/usr/local/hibernate-3.1"/> <target name="compile"> <javac srcdir="${src}" destdir="${build}" classpath="${hibernate.jar}" debug="true" > </javac> </target> <target name="run" depends="compile"> <java classname="hello.main" fork="true"> <classpath> <pathelement location="./"/> <pathelement location="${hibernate.jar}"/> <pathelement location="${mysqljdbc.jar}"/> <pathelement location="${dom4j.jar}"/> <pathelement location="${commons-logging.jar}"/> <pathelement location="${commons-collections.jar}"/> <pathelement location="${log4j.jar}"/> <pathelement location="c:/home/toshima/hibernate"/> <fileset dir="${hibernate.lib}"> <include name="**/*.jar"/> </fileset> </classpath> </java> </target> </project>

表示オプション

横に並べて表示:
変化行の前後のみ表示:
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。