Hi!
Habe mich gerade durch ein Tutorial durchgearbeitet und wollte nun selbiges "nachbauen", leider funktionierts nicht so wie ich das möchte..
Also ich möchte einfach nur testen ob das Zusammenspiel zwischen Hibernate und Spring passt sowie ob die DB angesprochen werden kann, als auch einen Satz in die DB schreiben.
Anmerken möchte ich auch noch dass ich bisher nur mit HBM Files gearbeitet habe und das mein erster Versuch mit Annotations ist.
Klasse Currency (repräsentiert eine Tabelle):
applicationContext.xml:
Testklasse:
Folgende Exception bekomme ich beim Ausführen:
Ich möchte noch keine DAOs schreiben etc nur einfach mal schnell versuchen ob alles klappt. Was mir auch noch auffällt ist, dass der Eclipse Editor bei
keine Autovervollständigung bietet - muss ich hierfür ein separates JAR einfügen?
mfg
Habe mich gerade durch ein Tutorial durchgearbeitet und wollte nun selbiges "nachbauen", leider funktionierts nicht so wie ich das möchte..
Also ich möchte einfach nur testen ob das Zusammenspiel zwischen Hibernate und Spring passt sowie ob die DB angesprochen werden kann, als auch einen Satz in die DB schreiben.
Anmerken möchte ich auch noch dass ich bisher nur mit HBM Files gearbeitet habe und das mein erster Versuch mit Annotations ist.
Klasse Currency (repräsentiert eine Tabelle):
Code:
package at.mpf.budget.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "currency")
public class Currency {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "shortcut")
private String shortcut;
@Column(name = "name")
private String name;
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public String getShortcut() {
return shortcut;
}
public void setShortcut(final String shortcut) {
this.shortcut = shortcut;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}
applicationContext.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="org.h2.Driver"></property>
<property name="jdbcUrl"
value="jdbc:h2:tcp://localhost/~/budget">
</property>
<property name="user" value="sa"></property>
<property name="password" value="sa"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
<list>
<value>at.mpf.budget.model.Currency</value>
</list>
</property>
<property name="hibernateProperties"></property>
<probs>
<prop key="hibernate.dialect">
org.hibernate.dialect.H2Dialect
</prop>
<prop key="hibernate.show.sql">true</prop>
<prop key="hierbante.format_sql">true</prop>
</props>
</bean>
</beans>
Testklasse:
Code:
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import at.mpf.budget.model.Currency;
public class TestHibernate2 {
/**
* @param args
*/
public static void main(String[] args) {
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration.configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Currency curr = new Currency();
curr.setName("Test Currency");
curr.setShortcut("TestS");
session.beginTransaction();
session.save(curr);
transaction.commit();
session.close();
sessionFactory.close();
}
}
Folgende Exception bekomme ich beim Ausführen:
Code:
28.05.2008 15:55:38 org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.1.GA
28.05.2008 15:55:38 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.6
28.05.2008 15:55:38 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
28.05.2008 15:55:38 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
28.05.2008 15:55:38 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
28.05.2008 15:55:38 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
28.05.2008 15:55:38 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1411)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1433)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:69)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1420)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:966)
at test.TestHibernate2.main(TestHibernate2.java:17)
Ich möchte noch keine DAOs schreiben etc nur einfach mal schnell versuchen ob alles klappt. Was mir auch noch auffällt ist, dass der Eclipse Editor bei
Code:
class="com.mchange.v2.c3p0.ComboPooledDataSource">
mfg
Zuletzt bearbeitet: