巴巴运动网 16 (产品分类实体对象基本属性的JPA映射)

这一节学出了好多debug,不过最后,都一一查出什么问题来了,学会找错误是一门大学问啊。



实体类.java


package com.itcast.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class ProductType {
	/** 类别 id **/
	private Integer typeid;

	/**类别名称 **/
	private String name;

	/** 搜索google 页面存放的 内容 **/
	private String note;

	/** 备注**/
	private boolean visible = true;

	@Id  @GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getTypeid() {
		return typeid;
	}

	public void setTypeid(Integer typeid) {
		this.typeid = typeid;
	}

	@Column(length=36,nullable=false)
	public String getName() {
		return name;
	}

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

	@Column(length=200)
	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	@Column(nullable=false)
	public boolean getVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

}



package com.itcast.service.product;

import com.itcast.bean.ProductType;

public interface ProductService {

	public void save(ProductType type);

}


接口实现类


package com.itcast.service.product.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itcast.bean.ProductType;
import com.itcast.service.product.ProductService;

@Service
@Transactional   
public class ProductServiceBean implements ProductService {

	@PersistenceContext  EntityManager em; 

	@Override
	public void save(ProductType type) {
		em.persist(type);
	}
}

beans.xml  【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"
       xmlns:context="http://www.springframework.org/schema/context"
       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/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<context:component-scan base-package="com.itcast"/>	
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	    <property name="driverClassName" value="${driverClassName}"/>
	    <property name="url" value="${url}"/>
	    <property name="username" value="${username}"/>
	    <property name="password" value="${password}"/>
	    <property name="initialSize" value="${initialSize}"/>
	    <property name="maxActive" value="${maxActive}"/>
	    <property name="maxIdle" value="${maxIdle}"/>
	    <property name="minIdle" value="${minIdle}"/>
  	</bean>	
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
			<property name="loadTimeWeaver">
	          <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
	       </property>
	</bean>

   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
   </bean>
	<!-- Activates @Transactional for DefaultImageDatabase -->
   <tx:annotation-driven transaction-manager="transactionManager"/>
	
</beans>

jdbc.properties文件

driverClassName=org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8
username=root
password=root
initialSize=1
maxActive=100
maxIdle=8
minIdle=1

persistence.xml 文件


<?xml version="1.0"?>
<persistence 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" version="1.0">
<!-- file:///D:\hibernate\hibernate-entitymanager-3.3.2.CR1\resources\org\hibernate\ejb\persistence_1_0.xsd -->
  <persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
  	<provider>org.hibernate.ejb.HibernatePersistence</provider>
	<properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
         <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
         <property name="hibernate.connection.username" value="root"/>
         <property name="hibernate.connection.password" value="123456"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
         <property name="hibernate.max_fetch_depth" value="3"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
	     <property name="hibernate.jdbc.fetch_size" value="18"/>
	     <property name="hibernate.jdbc.batch_size" value="10"/>
	     <property name="hibernate.show_sql" value="false"/>
	     <property name="hibernate.format_sql" value="false"/> 
      </properties>
  </persistence-unit>
</persistence>

测试类:【Spring最好是要:面向接口编程】

package junit.test;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.itcast.bean.ProductType;
import com.itcast.service.product.ProductService;

public class ProductTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test
	public void runtest() {
		
		ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
		ProductService productService = (ProductService) cxt.getBean("productServiceBean");
		ProductType type = new ProductType();
		type.setName("瑜伽用品");
		type.setNote("很好,不错");
		productService.save(type);
		
	}

}

1,报错:

Caused by: java.lang.UnsupportedOperationException: Not supported by BasicDataSource
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:899)
    at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:43)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
    at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
    at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
    at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
    at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1353)
    at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:38)
    at org.springframework.orm.jpa.DefaultJpaDialect.beginTransaction(DefaultJpaDialect.java:70)
    at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:330)
    ... 31 more



分析:

Not supported by BasicDataSource
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:899)
    at 

是因为:

<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
         <property name="hibernate.connection.username" value="root"/>
         <property name="hibernate.connection.password" value="123456"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>


定义了两次,导致jdbc两次加载。。。应删除。


2,报错:


如果编码不一致,也会报错,导致无法插入数据。


最后:测试成功。


不过,插入的数据在数据库中竟然是 ?正在解决中。。。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值