Maven 2 + Hibernate 3.2 + MySQL-Beispiel (Anmerkung)
Note
Dieser Artikel ist veraltet und einige Informationen sind in der neuesten Hibernate-Entwicklung nicht mehr gültig. Sie sollten sich auf das neueste Tutorial von -Maven 3 + Hibernate 3.6.3 + Oracle 11g Example (Annotation)beziehen.
In diesem Lernprogramm werden die vorherigenMaven 2 + Hibernate 3.2 + MySQL Example (XML mapping)geändert und die XML-Zuordnungsdatei für den Ruhezustand durch Anmerkungscode ersetzt.
In diesem Artikel verwendete Tools und Technologien:
-
Maven 2.2.1
-
JDK 1.6.0_13
-
Ruhezustand 3.2.3.GA
-
MySQL 5.0
1. Erstellen Sie eine Projektinfrastruktur
Erstellen Sie die Projektinfrastruktur inMaven + Hibernate (XML mapping file) + MySQL Example
2. Fügen Sie das JBoss-Repository hinzu
Das JBoss-Repository inpom.xml
ist erforderlich, um die Annotationsbibliothek für den Ruhezustand herunterzuladen.
JBoss repository http://repository.jboss.com/maven2/
3. Hinzufügen der Abhängigkeit von Ruhezustand-Annotationen
Fügen Sie inpom.xml
die Annotationen für den Ruhezustand und die Annotationen für den Ruhezustand hinzu.
hibernate-annotations hibernate-annotations 3.3.0.GA hibernate-commons-annotations hibernate-commons-annotations 3.0.0.GA
Datei: pom.xml
4.0.0 com.example.common HibernateExample jar 1.0-SNAPSHOT HibernateExample http://maven.apache.org JBoss repository http://repository.jboss.com/maven2/ mysql mysql-connector-java 5.1.9 hibernate hibernate3 3.2.3.GA hibernate-annotations hibernate-annotations 3.3.0.GA hibernate-commons-annotations hibernate-commons-annotations 3.0.0.GA dom4j dom4j 1.6.1 commons-logging commons-logging 1.1.1 commons-collections commons-collections 3.2.1 cglib cglib 2.2 javax.transaction jta 1.1
4. Projektklassenpfad aktualisieren
Geben Sie an der Eingabeaufforderung "mvn eclipse:eclipse
" ein, um die Abhängigkeitsbibliothek herunterzuladen und den Projektklassenpfad von Eclipse zu aktualisieren.
5. Aktualisieren Sie HibernateUtil.java
Aktualisieren Sie "HibernateUtil
", um "AnnotationConfiguration" anstelle von "Configuration" zum Erstellen der Hibernate-Sitzungsfactory zu verwenden.
Verwendet zuvor "Konfiguration" - Für den Ruhezustand XML-Zuordnungsdatei
return new Configuration().configure().buildSessionFactory();
Ändern Sie es in "AnnotationConfiguration" - Für die Unterstützung von Annotationen im Ruhezustand
return new AnnotationConfiguration().configure().buildSessionFactory();
Datei: HibernateUtil.java
package com.example.persistence; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new AnnotationConfiguration().configure().buildSessionFactory(); } 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); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void shutdown() { // Close caches and connection pools getSessionFactory().close(); } }
6. Modellklasse aktualisieren
Aktualisieren Sie "Stock.java", um die Annotation wie folgt zu verwenden:
Stock.java
package com.example.common; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.UniqueConstraint; @Entity @Table(name = "stock", catalog = "example", uniqueConstraints = { @UniqueConstraint(columnNames = "STOCK_NAME"), @UniqueConstraint(columnNames = "STOCK_CODE") }) public class Stock implements java.io.Serializable { private Integer stockId; private String stockCode; private String stockName; public Stock() { } public Stock(String stockCode, String stockName) { this.stockCode = stockCode; this.stockName = stockName; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "STOCK_ID", unique = true, nullable = false) public Integer getStockId() { return this.stockId; } public void setStockId(Integer stockId) { this.stockId = stockId; } @Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10) public String getStockCode() { return this.stockCode; } public void setStockCode(String stockCode) { this.stockCode = stockCode; } @Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20) public String getStockName() { return this.stockName; } public void setStockName(String stockName) { this.stockName = stockName; } }
7. Löschen Sie die vorhandene Hibernate-XML-Zuordnungsdatei
Löschen Sie die vorhandene XML-Zuordnungsdatei für den Ruhezustand - "Stock.hbm.xml". Dies ist nicht mehr erforderlich.
8. Aktualisieren Sie die Konfigurationsdatei für den Ruhezustand
Aktualisieren Sie die Hibernate-Konfigurationsdatei - hibernate.cfg.xml.
Zuvor hatte es die Hibernate-XML-Zuordnungsdatei mit dem Tag "mapping resource"
Ändern Sie es in Modellklasse mit dem Tag "Mapping-Klasse"
Datei: hibernate.cfg.xml
false com.mysql.jdbc.Driver password jdbc:mysql://localhost:3306/example root org.hibernate.dialect.MySQLDialect true
9. Projektstruktur überprüfen
Klingt so, als ob nur wenige Dateien geändert wurden. Überprüfen Sie diese und stellen Sie sicher, dass die Ordnerstruktur wie folgt lautet:
10. Führen Sie es aus und sehen Sie die Ausgabe
Führen Sie Ihre App.java aus und fügen Sie einen neuen Datensatz in die Tabelle "Stock" ein. Das Ergebnis sollte mit dem vorherigen Beispiel für eine Hibernate-XML-Zuordnungsdatei identisch sein.
Maven + Hibernate + MySQL ... Hibernate: insert into example.stock (STOCK_CODE, STOCK_NAME) values (?, ?)
Erledigt.
Laden Sie es herunter -Maven-Hibernate-annotation-MySQL-Example.zip (8 KB)