Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > Data Access

Reply
 
Thread Tools Display Modes
  #1  
Old Oct 12th, 2006, 05:25 AM
sliwa.andrzej sliwa.andrzej is offline
Junior Member
 
Join Date: Oct 2006
Posts: 7
Default Spring 2.0 Toplink - Unknown entity bean class : ... Marked @Entity ??

Hi I test this sample as j2se application

public static void main(String[] args) {
// TODO Auto-generated method stub

ApplicationContext ctx = new ClassPathXmlApplicationContext(”applicationContext .xml”);
RestaurantDao rdao = (RestaurantDao) ctx.getBean(”restaurantDao”);
List lst = rdao.findByName(”Dover Diner”);

And this works, but when i try use rdao.save … or findbyId, i have strange error

Exception in thread “main” org.springframework.dao.InvalidDataAccessApiUsageE xception: Unknown entity bean class: class blog.jpa.domain.Restaurant, please verify that this class has been marked with the @Entity annotation.; nested exception is java.lang.IllegalArgumentException: Unknown entity bean class: class blog.jpa.domain.Restaurant, please verify that this class has been marked with the @Entity annotation.
Caused by: java.lang.IllegalArgumentException: Unknown entity bean class: class blog.jpa.domain.Restaurant, please verify that this class has been marked with the @Entity annotation.
at oracle.toplink.essentials.internal.ejb.cmp3.base.E ntityManagerImpl.findInternal(EntityManagerImpl.ja va:228)
at oracle.toplink.essentials.internal.ejb.cmp3.Entity ManagerImpl.find(EntityManagerImpl.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.orm.jpa.ExtendedEntityManagerC reator$ExtendedEntityManagerInvocationHandler.invo ke(ExtendedEntityManagerCreator.java:237)
at $Proxy12.find(Unknown Source)
at org.springframework.orm.jpa.JpaTemplate$1.doInJpa( JpaTemplate.java:221)
at org.springframework.orm.jpa.JpaTemplate.execute(Jp aTemplate.java:183)
at org.springframework.orm.jpa.JpaTemplate.find(JpaTe mplate.java:219)
at blog.jpa.dao.JpaRestaurantDao.findById(JpaRestaura ntDao.java:11)
at blog.main.Main.main(Main.java:37)


Can anyone explain my what i doing wrong ??
I use LocalContainerEntityManagerFactoryBean in applicationContext.

Please send my working example j2se application, or explain what i doing wrong.

My files... :

package blog.jpa.domain;

import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;

@Entity
public class Restaurant {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private String name;

@OneToOne(cascade = CascadeType.ALL)
private Address address;

@ManyToMany
@JoinTable(inverseJoinColumns = @JoinColumn(name = "ENTREE_ID"))
private Set<Entree> entrees;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public Set<Entree> getEntrees() {
return entrees;
}

public void setEntrees(Set<Entree> entrees) {
this.entrees = entrees;
}

}

package blog.jpa.dao;

import java.util.List;
import blog.jpa.domain.Restaurant;

public interface RestaurantDao {

public Restaurant findById(long id);

public List<Restaurant> findByName(String name);

public List<Restaurant> findByStreetName(String streetName);

public List<Restaurant> findByEntreeNameLike(String entreeName);

public List<Restaurant> findRestaurantsWithVegetarianEntrees();

public void save(Restaurant restaurant);

public Restaurant update(Restaurant restaurant);

public void delete(Restaurant restaurant);

}


package blog.jpa.dao;

import java.util.List;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import blog.jpa.domain.Restaurant;

public class JpaRestaurantDao extends JpaDaoSupport implements RestaurantDao {

public Restaurant findById(long id) {
return getJpaTemplate().find(Restaurant.class, id);
}

public List<Restaurant> findByName(String name) {
return getJpaTemplate().find("select r from Restaurant r where r.name = ?1", name);
}

public List<Restaurant> findByStreetName(String streetName) {
return getJpaTemplate().find("select r from Restaurant r where r.address.streetName = ?1", streetName);
}

public List<Restaurant> findByEntreeNameLike(String entreeName) {
return getJpaTemplate().find("select r from Restaurant r where r.entrees.name like ?1", entreeName);
}

public List<Restaurant> findRestaurantsWithVegetarianEntrees() {
return getJpaTemplate().find("select r from Restaurant r where r.entrees.vegetarian = 'true'");
}

public void save(Restaurant restaurant) {
getJpaTemplate().persist(restaurant);
}

public Restaurant update(Restaurant restaurant) {
return getJpaTemplate().merge(restaurant);
}

public void delete(Restaurant restaurant) {
getJpaTemplate().remove(restaurant);
}

}

package blog.main;

import java.util.List;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlAp plicationContext;

import blog.jpa.dao.RestaurantDao;
import blog.jpa.domain.Address;
import blog.jpa.domain.Entree;
import blog.jpa.domain.Restaurant;

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext .xml");
RestaurantDao rdao = (RestaurantDao) ctx.getBean("restaurantDao");

Address addr = new Address();
addr.setStreetName("sportowa");
addr.setStreetNumber(10);

/*
Restaurant rest = new Restaurant();
rest.setAddress(addr);

rest.setName("My");

rdao.save(rest);*/
List<Restaurant> lst = rdao.findByName("Dover Diner");
//Restaurant rst = rdao.findById(1);


System.out.println("end");
}

}

<?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="restaurantDao" class="blog.jpa.dao.JpaRestaurantDao">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.TopLinkJ paVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="oracle.toplink.essentials.platform.database .DerbyPlatform"/>
</bean>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading .SimpleLoadTimeWeaver"/>
</property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="url" value="jdbc:derby://localhost:1527/Test;create=true"/>
<property name="username" value="sa"/>
<property name="password" value="sa"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>

</beans>

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

<persistence-unit name="SpringJpaGettingStarted" transaction-type="RESOURCE_LOCAL"/>

</persistence>
Reply With Quote
  #2  
Old Oct 12th, 2006, 06:08 AM
sliwa.andrzej sliwa.andrzej is offline
Junior Member
 
Join Date: Oct 2006
Posts: 7
Default Problem is in classloading.

Ok i found solution
i must change
loadtimeWeavera to InstrumentationLoadTimeWeaver
Reply With Quote
  #3  
Old Oct 28th, 2006, 08:39 AM
Jim Shingler Jim Shingler is offline
Junior Member
 
Join Date: Oct 2006
Posts: 6
Default Does Save / Persist actually work?

This helped me past an issue I was having, . . .

I tried the persist appears to work when main runs but when I look in the DB it isn't persisted.

I am using the exact same setup.

Thoughts
Reply With Quote
  #4  
Old Oct 28th, 2006, 11:14 AM
sliwa.andrzej sliwa.andrzej is offline
Junior Member
 
Join Date: Oct 2006
Posts: 7
Default Transaction !!!

The problem is in transaction.

You can use automatic transation defined in applicationContext (below)
or you can use this statment in your code:

DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.P ROPAGATION_REQUIRED);

TransactionStatus status = txManager.getTransaction(def);
try {
// execute your business logic here
pDao.save(p);
}
catch (Exception ex) {
txManager.rollback(status);
}
txManager.commit(status);

If you want use automatic (I prefer this method. You must define
your dao facade in my example personDao using txProxyTemplate.
Look on my applicationContext.xml and You see solution.

Best regards,

Andrzej Śliwa



tel: *************

gg: 3853320
_______________________________

email: sliwa.andrzej@gmail.com

http://sliwa-andrzej-devblog.blogspot.com/



<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
- <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
- <property name="jpaVendorAdapter">
- <bean class="org.springframework.orm.jpa.vendor.TopLinkJ paVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="oracle.toplink.essentials.platform.database .DerbyPlatform" />
</bean>
</property>
- <property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading .InstrumentationLoadTimeWeaver" />
</property>
</bean>
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url" value="jdbc:derby://localhost:1527/Database;create=true" />
<property name="username" value="user01" />
<property name="password" value="user01" />
</bean>
- <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
- <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="transactionManager" />
- <property name="transactionAttributes">
- <props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
- <!--
-->
- <bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
- <property name="transactionManager">
<ref bean="transactionManager" />
</property>
- <property name="transactionAttributes">
- <props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
- <!-- Proxy with automatic transaction support
-->
- <bean id="personDao" parent="txProxyTemplate">
<property name="proxyTargetClass" value="true" />
- <property name="target">
- <bean class="com.toplink.PersonDao">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</property>
</bean>
</beans>
Reply With Quote
  #5  
Old Oct 28th, 2006, 01:36 PM
Jim Shingler Jim Shingler is offline
Junior Member
 
Join Date: Oct 2006
Posts: 6
Thumbs up Many Thanks

That was it!

Many Thanks for the Tip!


I've been looking all over the place for a simple standalone /out of container transactional example and haven't been able to find one. But, . . . with your assistance, I think I have a working example.

I understand it at a conceptual level, . . . but not a detailed level.

Your Help is greatly appreciated
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 11:36 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.