1. 程式人生 > >巴巴運動網 16 (產品分類實體物件基本屬性的JPA對映)

巴巴運動網 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,報錯:

如果編碼不一致,也會報錯,導致無法插入資料。

最後:測試成功。


不過,插入的資料在資料庫中竟然是 ?正在解決中。。。