论坛首页 Java版 Spring

spring+toplink jpa 错误

浏览 1346 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2007-04-25
最近写了简单的JPA程序,但是出了个奇怪的错误,看看

Model类


package com.ibm.dw.spring2;

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="address")
public class Address {

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

	@Column(name = "NUM")
	private int number;

	@Column(name = "STNAME", length = 25)
	private String street;

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

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public Address(int number, String street) {
		this.number = number;
		this.street = street;
	}

	public Address() {
	}
}


接口
package com.ibm.dw.spring2;


public interface EmployeeService {

	public Address save(Address addr);	
		
}


实现类

package com.ibm.dw.spring2;

import org.springframework.orm.jpa.support.JpaDaoSupport;

public class EmployeeDAO extends JpaDaoSupport implements EmployeeService {
	
	public Address save(Address addr) {
		getJpaTemplate().persist(addr);
		return addr;
	}

	
}



spring配置文件

<?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="employeeService" parent="baseService">
		<property name="target">
			<bean class="com.ibm.dw.spring2.EmployeeDAO">
				<property name="entityManagerFactory" ref="entityManagerFactory"/>
			</bean>
		</property>
	</bean>

	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory"/>
	    <property name="dataSource" ref="dataSource"/>		
	</bean>
	
	<bean id="baseService" lazy-init="true" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager">
			<ref bean="transactionManager"/>
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="insert*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		
	    <property name="dataSource" ref="dataSource"/>		
	    <property name="jpaVendorAdapter">
	       <bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
	          <property name="showSql" value="true"/>
         	  <property name="generateDdl" value="true"/>	 
         	  <property name="databasePlatform" value="oracle.toplink.essentials.platform.database.SQLServerPlatform"/> 
	       </bean>
	    </property>
		
	    <property name="loadTimeWeaver">
	       <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
	    </property>
		
	</bean>
		
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	   <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
	   <property name="url" value="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=jpa"/>
	   <property name="username" value="sa" />
	   <property name="password" value="sa" />      
	</bean>	
</beans>


测试类

package com.ibm.dw.spring2;

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

public class Test {
	
	public static void main(String[] args){
		
     try {
			 ApplicationContext context = new ClassPathXmlApplicationContext("service.xml");
			 
			 EmployeeService service = (EmployeeService)context.getBean("employeeService");
			 Address addr = new Address(10, "Walker Street");
			 service.save(addr);
			 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}


错误信息

log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
log4j:WARN Please initialize the log4j system properly.
[TopLink Config]: 2007.04.25 11:17:57.703--ServerSession(14440411)--Thread(Thread[main,5,main])--The alias name for the entity class [class com.ibm.dw.spring2.Address] is being defaulted to: Address.
[TopLink Config]: 2007.04.25 11:17:57.843--ServerSession(14440411)--Thread(Thread[main,5,main])--The column name for element [private long com.ibm.dw.spring2.Address.id] is being defaulted to: ID.
[TopLink Info]: 2007.04.25 11:17:58.031--ServerSession(14440411)--Thread(Thread[main,5,main])--TopLink, version: Oracle TopLink Essentials - 2.0 (Build b43-beta3 (04/18/2007))
[TopLink Config]: 2007.04.25 11:17:58.046--ServerSession(14440411)--Connection(11372121)--Thread(Thread[main,5,main])--connecting(DatabaseLogin(
	platform=>SQLServerPlatform
	user name=> ""
	connector=>JNDIConnector datasource name=>null
))
[TopLink Config]: 2007.04.25 11:17:58.218--ServerSession(14440411)--Connection(3969559)--Thread(Thread[main,5,main])--Connected: jdbc:jtds:sqlserver://localhost:1433;DatabaseName=jpa
	User: sa
	Database: Microsoft SQL Server  Version: 08.00.0760
	Driver: jTDS Type 4 JDBC Driver for MS SQL Server and Sybase  Version: 1.2
[TopLink Config]: 2007.04.25 11:17:58.218--ServerSession(14440411)--Connection(10175206)--Thread(Thread[main,5,main])--connecting(DatabaseLogin(
	platform=>SQLServerPlatform
	user name=> ""
	connector=>JNDIConnector datasource name=>null
))
[TopLink Config]: 2007.04.25 11:17:58.218--ServerSession(14440411)--Connection(21307627)--Thread(Thread[main,5,main])--Connected: jdbc:jtds:sqlserver://localhost:1433;DatabaseName=jpa
	User: sa
	Database: Microsoft SQL Server  Version: 08.00.0760
	Driver: jTDS Type 4 JDBC Driver for MS SQL Server and Sybase  Version: 1.2
[TopLink Info]: 2007.04.25 11:17:58.359--ServerSession(14440411)--Thread(Thread[main,5,main])--file:/D:/eclipse3.2/workspace/Jpa/WebContent/WEB-INF/classes/-default login successful
org.springframework.dao.InvalidDataAccessApiUsageException: Object: com.ibm.dw.spring2.Address@b60b93 is not a known entity type.; nested exception is java.lang.IllegalArgumentException: Object: com.ibm.dw.spring2.Address@b60b93 is not a known entity type.
Caused by: java.lang.IllegalArgumentException: Object: com.ibm.dw.spring2.Address@b60b93 is not a known entity type.
	at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:3198)
	at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:190)
	at org.springframework.orm.jpa.JpaTemplate$5.doInJpa(JpaTemplate.java:263)
	at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:191)
	at org.springframework.orm.jpa.JpaTemplate.persist(JpaTemplate.java:261)
	at com.ibm.dw.spring2.EmployeeDAO.save(EmployeeDAO.java:10)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:281)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:187)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:154)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
	at $Proxy9.save(Unknown Source)
	at com.ibm.dw.spring2.Test.main(Test.java:15)
   
时间:2007-05-20
Object: com.ibm.dw.spring2.Address@b60b93 is not a known entity type.; nested exception is java.lang.IllegalArgumentException: Object: com.ibm.dw.spring2.Address@b60b93 is not a known entity type.

你的测试用例写的不对,直接 extends AbstractJpaTests 就行了。
package com.ibm.dw.spring2;

import java.util.Date;
import org.springframework.test.jpa.AbstractJpaTests;
import com.ibm.dw.spring2jpa.Address;
import com.ibm.dw.spring2jpa.Employee;

public class Test extends AbstractJpaTests
{
	private EmployeeService employeeService;

	public void setEmployeeService(EmployeeService employeeService)
	{
		this.employeeService = employeeService;
	}

	protected String[] getConfigLocations()
	{
		return new String[]{"classpath:service.xml"};
	}

	protected void onSetUpInTransaction() throws Exception
	{
	}

	public void testAdd()
	{
		Address addr = new Address(10,"Walker Street");
		employeeService.save(addr);
	}
}
   
0 请登录后投票
论坛首页 Java版 Spring

跳转论坛:
JavaEye推荐