Back Forum Reply New

Problem using TopLink with Spring

Hello all
I've been trying out the tutorial from Interface21 which uses TopLink for persisting framework.

The problem is that the unit tests go through, but when i create a Lookup class for creating the dao, they fail on persisting the domain objects.
The Restaurant domain object:

Code:
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 = quot;ENTREE_IDquot;))
private Setlt;Entreegt; 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 Setlt;Entreegt; getEntrees() {
return entrees;
}

public void setEntrees(Setlt;Entreegt; entrees) {
this.entrees = entrees;
}

}
the dao:

Code:
package blog.jpa.dao;

import java.util.List;

import org..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 Listlt;Restaurantgt; findByName(String name) {
return getJpaTemplate().find(quot;select r from Restaurant r where r.name = ?1quot;, name);
}

public Listlt;Restaurantgt; findByStreetName(String streetName) {
return getJpaTemplate().find(quot;select r from Restaurant r where r.address.streetName = ?1quot;, streetName);
}

public Listlt;Restaurantgt; findByEntreeNameLike(String entreeName) {
return getJpaTemplate().find(quot;select r from Restaurant r where r.entrees.name like ?1quot;, entreeName);
}

public Listlt;Restaurantgt; findRestaurantsWithVegetarianEntrees() {
return getJpaTemplate().find(quot;select r from Restaurant r where r.entrees.vegetarian = 'true'quot;);
}

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);
}
}
application context:
Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans xmlns=quot;schema/beansquot; xmlns:xsi=quot;2001/XMLSchema-instancequot; xsi:schemaLocation=quot;schema/beans   schema/beans/spring-beans.xsdquot;gt;
lt;bean id=quot;restaurantDaoquot; class=quot;blog.jpa.dao.JpaRestaurantDaoquot;gt;   lt;property name=quot;entityManagerFactoryquot; ref=quot;entityManagerFactoryquot;/gt; lt;/beangt;
lt;bean id=quot;entityManagerFactoryquot; class=quot;org..orm.jpa.LocalContainerEntityManagerFactoryBeanquot;gt;   lt;property name=quot;dataSourcequot; ref=quot;dataSourcequot;/gt;   lt;property name=quot;jpaVendorAdapterquot;gt;     lt;bean class=quot;org..orm.jpa.vendor.TopLinkJpaVendorAdapterquot;gt;       lt;property name=quot;showSqlquot; value=quot;truequot;/gt;       lt;property name=quot;generateDdlquot; value=quot;truequot;/gt;       lt;property name=quot;databasePlatformquot; value=quot;oracle.toplink.essentials.platform.database.HSQLPlatformquot;/gt;     lt;/beangt;   lt;/propertygt;   lt;property name=quot;loadTimeWeaverquot;gt;     lt;bean class=quot;org..instrument.classloading.SimpleLoadTimeWeaverquot;/gt;   lt;/propertygt; lt;/beangt;
lt;bean id=quot;dataSourcequot; class=quot;org..jdbc.datasource.DriverManagerDataSourcequot;gt;   lt;property name=quot;driverClassNamequot; value=quot;org.hsqldb.jdbcDriverquot;/gt;   lt;property name=quot;uclquot; value=quot;jdbc:hsqldb:mem:testquot;/gt;   lt;property name=quot;usernamequot; value=quot;saquot;/gt;   lt;property name=quot;passwordquot; value=quot;quot;/gt; lt;/beangt;
lt;bean id=quot;transactionManagerquot; class=quot;org..orm.jpa.JpaTransactionManagerquot;gt;   lt;property name=quot;entityManagerFactoryquot; ref=quot;entityManagerFactoryquot;/gt;   lt;property name=quot;dataSourcequot; ref=quot;dataSourcequot;/gt; lt;/beangt;

lt;/beansgt;
persistence.xml

Code:
lt;persistence xmlns=quot;xml/ns/persistencequot; version=quot;1.0quot;gt;
lt;persistence-unit name=quot;SpringJpaGettingStartedquot; transaction-type=quot;RESOURCE_LOCALquot; /gt;
lt;/persistencegt;
The lookup class:

Code:
package blog.jpa.dao;

import org..context.ApplicationContext;
import org..context.support.ClassPathXmlApplicationContext;

import blog.jpa.domain.Address;
import blog.jpa.domain.Restaurant;

public class DaoLookup {
private static final String CONTEXT_PATH=quot;classpath:/blog/jpa/dao/applicationContext.xmlquot;;

private static ApplicationContext context;

static {
context = new ClassPathXmlApplicationContext(CONTEXT_PATH);
}

public static RestaurantDao getRestaurantDao(){
return (RestaurantDao)context.getBean(quot;restaurantDaoquot;);
}
}
class for testing (not TestCase):

Code:
package blog.jpa.dao;

import blog.jpa.domain.Address;
import blog.jpa.domain.Restaurant;

public class LookupTest {

/*** @param args*/
public static void main(String[] arg){
RestaurantDao dao = DaoLookup.getRestaurantDAO();
Restaurant r = new Restaurant();
Address a = new Address();
a.setStreetName(quot;Dover Streetquot;);
a.setStreetNumber(23);
r.setAddress(a);
r.setName(quot;TestRestaurantquot;);
dao.save(r);
System.out.println(r.getId());
}

}
The dao throws an exception when calling save:

Code:
Exception in thread quot;mainquot; org..dao.InvalidDataAccessApiUsageException: Object: blog.jpa.domain.Restaurant@322bce is not a known entity type.; nested exception is java.lang.IllegalArgumentException: Object: blog.jpa.domain.Restaurant@322bce is not a known entity type.
Caused by: java.lang.IllegalArgumentException: Object: blog.jpa.domain.Restaurant@322bce is not a known entity type.
at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:3178)
at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:149)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org..orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:283)
at $Proxy12.persist(Unknown Source)
at org..orm.jpa.JpaTemplate$5.doInJpa(JpaTemplate.java:263)
at org..orm.jpa.JpaTemplate.execute(JpaTemplate.java:191)
at org..orm.jpa.JpaTemplate.persist(JpaTemplate.java:261)
at blog.jpa.dao.JpaRestaurantDao.save(JpaRestaurantDao.java:32)
at blog.jpa.dao.LookupTest.main(LookupTest.java:19)
When i try to use Kodo instead of Toplink everything works great, but i would prefer using TopLink

Do someone know what may be the problem?
Thankful for any help i can get

/Per-Jarle
¥
Back Forum Reply New