Hallo,
ich versuche gerade via Spring, JPA und Hibernate eine DB-Verbindung hinzubekommen. Allerdings bekomme ich beim Starten immer die Meldung: java.lang.IllegalArgumentException: Unknown entity bar.domain.Drink
Ich verstehe das einfach nicht! Was mache ich falsch!?
Meine persistence.xml
[XML]
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="bar" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.use_sql_comments" value="false"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.cache.use_query_cache" value="false"/>
<property name="hibernate.cache.use_second_level_cache" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
[/XML]
Meine Spring-Konfiguration:
[XML]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
Index of /schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Index of /schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
Index of /schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
Index of /schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--
Scan classpath for Spring components starting at base-package.
Automatically inludes:
- AutowiredAnnotationBeanPostProcessor
- CommonAnnotationBeanPostProcessor
-->
<context:component-scan base-package="bar">
<context:include-filter type="regex" expression=".dao|.app|.jpa"/>
</context:component-scan>
<!-- Enable processing of @PersistenceContext and @PersistenceUnit -->
<context:annotation-config/>
<!-- Enable transaction configuration with @Transactional -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Configure a c3p0 pooled data source -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="sa"/>
<property name="password" value=""/>
<property name="driverClass" value="org.hsqldb.jdbcDriver"/>
<property name="jdbcUrl" value="jdbc:hsqldb:mem:."/>
<property name="initialPoolSize" value="1"/>
<property name="minPoolSize" value="1"/>
<property name="maxPoolSize" value="10"/>
</bean>
<!-- Configure the JPA entity manager factory with Hibernate -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false"/>
<property name="database" value="HSQL"/>
<property name="generateDdl" value="true"/>
</bean>
</property>
<property name="persistenceUnitName" value="bar"/>
</bean>
<!-- Configure transaction manager for JPA -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
</beans>
[/XML]
Meine Dao:
Meine Entity:
ich versuche gerade via Spring, JPA und Hibernate eine DB-Verbindung hinzubekommen. Allerdings bekomme ich beim Starten immer die Meldung: java.lang.IllegalArgumentException: Unknown entity bar.domain.Drink
Ich verstehe das einfach nicht! Was mache ich falsch!?
Meine persistence.xml
[XML]
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="bar" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.use_sql_comments" value="false"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.cache.use_query_cache" value="false"/>
<property name="hibernate.cache.use_second_level_cache" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
[/XML]
Meine Spring-Konfiguration:
[XML]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
Index of /schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Index of /schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
Index of /schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
Index of /schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--
Scan classpath for Spring components starting at base-package.
Automatically inludes:
- AutowiredAnnotationBeanPostProcessor
- CommonAnnotationBeanPostProcessor
-->
<context:component-scan base-package="bar">
<context:include-filter type="regex" expression=".dao|.app|.jpa"/>
</context:component-scan>
<!-- Enable processing of @PersistenceContext and @PersistenceUnit -->
<context:annotation-config/>
<!-- Enable transaction configuration with @Transactional -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Configure a c3p0 pooled data source -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="sa"/>
<property name="password" value=""/>
<property name="driverClass" value="org.hsqldb.jdbcDriver"/>
<property name="jdbcUrl" value="jdbc:hsqldb:mem:."/>
<property name="initialPoolSize" value="1"/>
<property name="minPoolSize" value="1"/>
<property name="maxPoolSize" value="10"/>
</bean>
<!-- Configure the JPA entity manager factory with Hibernate -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false"/>
<property name="database" value="HSQL"/>
<property name="generateDdl" value="true"/>
</bean>
</property>
<property name="persistenceUnitName" value="bar"/>
</bean>
<!-- Configure transaction manager for JPA -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
</beans>
[/XML]
Meine Dao:
Java:
package bar.dao.jpa;
import bar.dao.IDrinkDao;
import bar.domain.Drink;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;
import java.util.List;
import org.springframework.stereotype.Repository;
import org.springframework.context.annotation.Scope;
@Repository("drinkDao")
@Scope("singleton")
public class JpaDrinkDao implements IDrinkDao
{
@PersistenceContext
private EntityManager em;
public Drink createNew() {
return new Drink();
}
public void persist(Drink drink) {
em.persist(drink);
}
@SuppressWarnings({"unchecked"})
public List<Drink> findAll() {
return em.createQuery("select d from Drink d").getResultList();
}
public Drink findById(long id) {
return em.find(Drink.class, id);
}
public void delete(Drink drink) {
em.remove(drink);
}
}
Meine Entity:
Java:
package bar.domain;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.persistence.*;
@Entity
@Table(name = "DRINK")
public class Drink extends AbstractBaseEntity
{
private Long _ID;
private String _Name;
private Integer _Percent;
public Drink()
{
super();
}
public Drink(String strName, Integer iPercent)
{
_Name = strName;
_Percent = iPercent;
}
@Id
@TableGenerator(name="DrinkPKGen", table="PK_GEN",
pkColumnName="GEN_KEY", valueColumnName="GEN_Value", pkColumnValue="DRINK_ID")
@GeneratedValue(strategy=GenerationType.TABLE, generator="DrinkPKGen")
@Column(name = "ID")
public Long getId()
{
return _ID;
}
public void setId(Long iID)
{
_ID = iID;
}
public void setName(String strName)
{
_Name = strName;
}
@Column(name = "NAME")
@NotNull
public String getName()
{
return _Name;
}
public void setPercent(Integer iPercent)
{
_Percent = iPercent;
}
@Column(name = "PERCENT")
@Min(value = 0)
@Max(value = 100)
public Integer getPercent()
{
return _Percent;
}
}