巴巴运动网 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容器】




		
    
    
    
	    
	    
	    
	    
	    
	    
	    
	    
  		
	
		
		
			
	          
	       
	

   
        
   
	
   
	

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 文件





  
  	org.hibernate.ejb.HibernatePersistence
	
         
         
         
         
         
         
         
	     
	     
	     
	      
      
  


测试类:【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 

是因为:


         
         
         


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


2,报错:


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


最后:测试成功。


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





你可能感兴趣的:(【项目经验】)