日期:2014-05-20  浏览次数:20662 次

JPA Hibernate整合错误~
[color=#FF0000][/color]JPA配置
XML code

<?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">
  <persistence-unit name="library" 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/library?useUnicode=true&amp;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="3"/>
         <property name="hibernate.jdbc.batch_size" value="10"/>
         <property name="hibernate.show_sql" value="true"/>
         <property name="hibernate.format_sql" value="false"/>
      </properties>
  </persistence-unit>
</persistence>




Spring 配置
XML code

<context:component-scan base-package="edu.jmu"/>
  
  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
      <property name="persistenceUnitName" value="library"/>
  </bean>  

  <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory"/>
  </bean>
  
  <tx:annotation-driven transaction-manager="txManager"/>


Java code

@Entity
public class User implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = -2491807508669383375L;
    private int id;
    private String name;
    private String password;
    private Set<BorrowBook> borrowBooks;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Column(length = 10)
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Column(length = 10)
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},mappedBy="user",fetch = FetchType.LAZY )    
    public Set<BorrowBook> getBorrowBooks() {
        return borrowBooks;
    }
    public void setBorrowBooks(Set<BorrowBook> borrowBooks) {
        this.borrowBooks = borrowBooks;
    }
    
}


Java code

package edu.jmu.bean;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

public class BorrowBook implements java.io.Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = -6773524592176308024L;
    private int id;
    private User user;
    private int bookId;
    private Date date;
    
    private Book book;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    
    @Column()
    public int getBookId() {
        return bookId;
    }
    public void setBookId(int bookId) {
        this.bookId = bookId;
    }
    @Temporal(TemporalType.DATE)
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "userId", nullable = false)
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    
    public Book getBook() {
        return book;
    }
    public void setBook(Book book) {
        this.book = book;
    }
}