Soon,
i moved server, ASAP i will put it here. With all projects.
Soon,
i moved server, ASAP i will put it here. With all projects.
I start everything from “0”.
Best Regards.
Example 1;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version=”1.0″ encoding=”UTF-8″?> <!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd“> <hibernate-mapping> <class name=”com.kutayzorlu.entity.Adres” table=”adres”> <id name=”id” column=”id”> <generator class=”increment”/> </id> <property name=”adres1″ type=”string” length=”50″ column=”adres1″ /> <property name=”adres2″ type=”string” length=”50″ column=”adres2″ /> <property name=”zipCode” type=”string” length=”5″ column=”zip_code” /> <many-to-one name=”kisi” column=”kisi_id” not-null=”true” class=”com.kutayzorlu.entity.Kisi” /> </class> </hibernate-mapping> |
Example 2;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version=”1.0″ encoding=”UTF-8″?> <!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd“> <hibernate-mapping> <class name=”com.kutayzorlu.entity.Kisi” table=”kisi”> <id name=”id” column=”id”> <generator class=”increment”/> </id> <property name=”adi” type=”string” column=”adi” length=”25″ /> <property name=”soyadi” type=”string” column=”soyadi” length=”25″ /> <property name=”evTelefonu” type=”string” column=”ev_telefonu” length=”15″ /> <property name=”cepTelefonu” type=”string” column=”cep_telefonu” length=”15″ /> <property name=”mail” type=”string” column=”mail” length=”50″ /> <set name=”adresler” cascade=”all” lazy=”true”> <key column=”kisi_id”/> <one-to-many class=”com.kutayzorlu.entity.Adres” /> </set> </class> </hibernate-mapping> |
Example 3;
1- Create a netbeans JAVA project.
2- Add Library “Hibernate JPA ”
3- Right click project > Create new Hibernate Config > Hibernate.cfg
Write Connection parameters then test your Connection.
Connection Properties, Then create “Hibernate…cfg”
This hibernate configuration file location usually located at the root directory.
For example
” javakLibsrcHibernate.cfg”
4- We need to create .Class for the Fields of the database.Table.
There are 2 options
First : is set the fields by XML file
Second : is set the fields properties inside of java file. Which method name is Annotation
Example .java Class for the Fields
package hibernate; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Table; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.UniqueConstraint; @Entity @Table(name="tmp_hibernate",uniqueConstraints={@UniqueConstraint(columnNames={"id"})}) public class customer implements Serializable { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="id", nullable=false, unique=true, length=11) private Integer id; @Column(name="bayi_kodu", length=20, nullable=true) private int bayi_kodu; @Column(name="email", length=20, nullable=true) private String email; @Column(name="uname", length=20, nullable=true) private String username; public int getid() { return this.id; } public void setid(int id) { this.id = id; }
With this annotation file we need to set location of this file as a mapping inside of Hibernate |Config .
Package name is hibernate
Class name is customer
org.hibernate.dialect.MySQLDialect com.mysql.jdbc.Driver jdbc:mysql://xxxxxx:3306/DatabaseNAME?zeroDateTimeBehavior=convertToNull USERNAME PASSWORD thread
If you are not using annotation method, you need to define Field types inside of .xml file then you need to give xml path to Hibernate.cfg file
BuildSessionFactory
Example Util.java for hibernate
package hibernate; import java.util.Properties; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; public class util { private static SessionFactory sessionFactory = buildSessionFactory(); /** * @return */ private static SessionFactory buildSessionAnnotationFactory() { try { // Create the SessionFactory from hibernate.cfg.xml Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); System.out.println("Hibernate Annotation Configuration loaded"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); System.out.println("Hibernate Annotation serviceRegistry created"); SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); return sessionFactory; } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } private static SessionFactory sessionAnnotationFactory; public static SessionFactory getSessionAnnotationFactory() { if(sessionAnnotationFactory == null) sessionAnnotationFactory = buildSessionAnnotationFactory(); return sessionAnnotationFactory; } public static void shutdown() { // Close caches and connection pools sessionFactory.close(); } public static SessionFactory getSessionFactory() { if(sessionFactory == null) sessionFactory = buildSessionFactory(); return sessionFactory; } }
5- Testing,
import hibernate.customer; import hibernate.util; import org.hibernate.Session; import net.sf.ehcache.hibernate.HibernateUtil; import org.hibernate.SessionFactory; hibernate.customer Member_YENI = new customer(); Member_YENI.setbayi_kodu(14); Member_YENI.setemail("coder@gmail.com"); Member_YENI.setusername("kutayzorlu"); SessionFactory sessionFactory = hibernate.util.getSessionAnnotationFactory(); Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); session.save(Member_YENI); session.getTransaction().commit(); // This is Static defined inside of utility util.shutdown();
6- Hibernate Log
debug: Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistenceantlr-2.7.6.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistenceasm.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistenceasm-attrs.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistencecglib-2.1.3.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistencecommons-collections-2.1.1.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistenceorg-apache-commons-logging.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistencedom4j-1.6.1.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistenceehcache-1.2.3.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistencejdbc2_0-stdext.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistencejta.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistencehibernate3.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistencehibernate-tools.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistencehibernate-annotations.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistencehibernate-commons-annotations.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistencehibernate-entitymanager.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistencejavassist.jar" provided. Non-existing path "C:UsersDeutschDesktopjavakLiblibhibernate-persistenceejb3-persistence.jar" provided. Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistenceantlr-2.7.6.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistenceasm.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistenceasm-attrs.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistencecglib-2.1.3.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistencecommons-collections-2.1.1.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistenceorg-apache-commons-logging.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistencedom4j-1.6.1.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistenceehcache-1.2.3.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistencejdbc2_0-stdext.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistencejta.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistencehibernate3.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistencehibernate-tools.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistencehibernate-annotations.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistencehibernate-commons-annotations.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistencehibernate-entitymanager.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistencejavassist.jar Have no file for C:UsersDeutschDesktopjavakLiblibhibernate-persistenceejb3-persistence.jar started 0:6:8 May 22, 2015 12:06:09 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManagerINFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final} May 22, 2015 12:06:09 AM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.3.1.Final} May 22, 2015 12:06:09 AM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found May 22, 2015 12:06:09 AM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist May 22, 2015 12:06:09 AM org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: hibernate.cfg.xml May 22, 2015 12:06:09 AM org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: hibernate.cfg.xml May 22, 2015 12:06:09 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! Hibernate Configuration loaded May 22, 2015 12:06:09 AM org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null Hibernate serviceRegistry created May 22, 2015 12:06:09 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!) May 22, 2015 12:06:09 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://:3306/k?zeroDateTimeBehavior=convertToNull] May 22, 2015 12:06:09 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000046: Connection properties: {user=*******, password=****} May 22, 2015 12:06:09 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000006: Autocommit mode: false May 22, 2015 12:06:09 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 20 (min=1) May 22, 2015 12:06:10 AM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect May 22, 2015 12:06:10 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) May 22, 2015 12:06:10 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory INFO: HHH000397: Using ASTQueryTranslatorFactory May 22, 2015 12:06:11 AM org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: hibernate.cfg.xml May 22, 2015 12:06:11 AM org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: hibernate.cfg.xml May 22, 2015 12:06:11 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! Hibernate Annotation Configuration loaded May 22, 2015 12:06:11 AM org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null Hibernate Annotation serviceRegistry created May 22, 2015 12:06:11 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!) May 22, 2015 12:06:11 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL May 22, 2015 12:06:11 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator May 22, 2015 12:06:11 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000006: Autocommit mode: false May 22, 2015 12:06:11 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 20 (min=1) May 22, 2015 12:06:12 AM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect May 22, 2015 12:06:12 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) May 22, 2015 12:06:12 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory INFO: HHH000397: Using ASTQueryTranslatorFactory Ended0:6:41
7- Performance
Totally token 30 seconds, First initialization. But after 30 seconds, every update insert delete taking lower than 1 second.
* Hibernate veri sorgulama(data query) ve veri çekme(data retriaval) işlemlerini icinde barindirir.
* Hibernate geliştirme kolaylığı ve zamandan kazanç sağlar.
* Hibernate kullanımı olmadan tüm adı anılan işlemler için SQL ve JDBC’nin olanaklarından faydalanılarak el ile(manual) veri işleme(data handling) gerçekleştirilmesi vakit kaybIdIr .
Guzel bulmus oldugum iyi bir calismayi, Sizinle paylasmak istedim.
Bu dosya Hacettepe universitesinin internet sayfasindan indirilmistir.
Hazırlayan : Adem Öztürk << Tesekkur ederim kendisine. İÇİNDEKİLER: 1. GİRİŞ: .................................................................................................................................... 3 2. HİBERNATE ÇALIŞMA İLKELERİ VE MİMARİSİ......................................................... 4 3. SESSIONFACTORY YAPILANDIRMASI............................................................................ 6 3.1 Programlama yoluyla yapılandırma ................................................................................. 6 3.2 SessionFactory elde etme ................................................................................................. 7 3.3 JDBC baglantıları............................................................................................................. 7 3.4 XML Yapılandırma Kütüğü............................................................................................. 7 4. KALICI SINIFLAR (PERSISTENT CLASSES) .................................................................. 8 4.1 Kalıcı sınıfların temel özellikleri.................................................................................... 10 4.2 Kalıcı sınıflarda ayırıcı (identifier) alanı ve Veri tabanı birincil anahtar niteliği ilişkisi .............................................................................................................................................. 10 4.3 Final tanımlanmış sınıflar ve türemiş sınıflar ................................................................ 10 4.4 equals() , hashCode() metotları hakkında ...................................................................... 10 4.5 Güncelleme, silme önlemleri ve yaşam döngüsü ara yüzü ............................................ 11 5. BASİT NESNE/İLİŞKİSEL MODEL EŞLEME (OBJECT/RELATİONAL MAPPİNG). 11 5.1 Nesne/ilişkisel model eşleme(Object/Relational Mapping) kütüğü içeriği.................... 14 5.1.1 Doctype ................................................................................................................... 14 5.1.2 hibernate-mapping................................................................................................... 14 5.1.3 class......................................................................................................................... 14 5.1.4 id.............................................................................................................................. 15 5.1.5 property ................................................................................................................... 16 5.1.6 many-to-one ............................................................................................................ 16 5.1.7 one-to-one................................................................................................................ 17 5.1.8 subclass.................................................................................................................... 17 6. KALICI VERİNİN İŞLENMESİ (MANİPULATİNG PERSİSTENT DATA ) ................. 17 6.1. Kalıcı nesnenin oluşturulması ....................................................................................... 17 6.2 Kalıcı nesnenin geri yüklenmesi(loading of a persistent object) ................................... 18 6.3 Sorgulama (Querying).................................................................................................... 19 6.4 Güncelleme ve Silme ..................................................................................................... 20 7. ÖRNEK UYGULAMALAR................................................................................................ 21 7.1 Örnek uygulama 1 .......................................................................................................... 21 7.1.1 eşleme (mapping) kütükleri..................................................................................... 21 7.1.2 Hibernate.properties kütüğü.................................................................................... 22 7.1.3 Şema oluşturma ....................................................................................................... 23 7.1.4 Kalıcı sınıfları oluşturma......................................................................................... 23 7.1.5 Hibernate ile Kalıcı Sınıfları Kullanma .................................................................. 23
Convention over configuration (also known as coding by convention) is a software design paradigm which seeks to decrease the number of decisions that developers need to make, gaining simplicity, but not necessarily losing flexibility.
The phrase essentially means a developer only needs to specify unconventional aspects of the application. For example, if there is a class Sale in the model, the corresponding table in the database is called “sales” by default. It is only if one deviates from this convention, such as calling the table “product sales”, that one needs to write code regarding these names.
When the convention implemented by the tool matches the desired behavior, it behaves as expected without having to write configuration files. Only when the desired behavior deviates from the implemented convention is explicit configuration required.