1

我正在開發一個接受客戶訂單的系統。 在系統中有以下類。多對多關係和複合主鍵與Spring MVC和Hibernate中的附加列

class product{ 

private Integer productId; 
//other attributes and methods 

} 

class OrderDetail{ 

private Integer orderId; 
//other attributes and methods 

} 

OrderDetail可以有很多產品和product可以在許多訂單。 另外,我想訂購的每個產品的數量。 所以我想這.....

order_id | product_id | quantity_of_each_product | 
    1 |  A1  | 10 
    1 |  B1  | 08 
    2 |  A1  | 04 
    2 |  B1  | 04 

我想這樣的一個表....

我是指一個例子,並且針對該table.It一個新的主鍵是OrderProductId類。然後我創建一個名爲OrderProduct的新實體類。

我也在使用JpaRepository類與DB連接。

這是我做了什麼......(所有代碼)

//this is my order class 
@Entity 
public class OrderDetail { 

    @Id 
    @GeneratedValue 
    private Integer orderId; 

//this is how I map with OrderProduct class 
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.orderDeatail", cascade=CascadeType.ALL) 
    Set<OrderProduct> orderProduct = new HashSet<OrderProduct>(); 

} 



//this is mys product class 
@Entity 
public class Product { 

    @Id 
    @GeneratedValue 
    private Integer id; 
//this is how I map with OrderProduct class 
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.product", cascade=CascadeType.ALL) 
    Set<OrderProduct> orderProduct = new HashSet<OrderProduct>(); 

} 


//this is my new primary key class 

@Embeddable 
public class OrderProductId implements Serializable { 

    @ManyToOne 
    private Product product; 
    @ManyToOne 
    private OrderDetail detail; 

} 

// this is my new entity class 

@Entity 
@Table(name = "order_product_quntity") 
@AssociationOverrides({ 
     @AssociationOverride(name = "pk.orderDeatail", 
      joinColumns = @JoinColumn(name = "ORDER_ID")), 
     @AssociationOverride(name = "pk.product", 
      joinColumns = @JoinColumn(name = "PRODUCT_ID")) }) 
public class OrderProduct { 

    private OrderProductId primaryKey = new OrderProductId(); 

    @Column(name = "Quntity") 
    private Integer quntity; 

    @EmbeddedId 
    public OrderProductId getPrimaryKey() { 
     return primaryKey; 
    } 

    public void setPrimaryKey(OrderProductId primaryKey) { 
     this.primaryKey = primaryKey; 
    } 

    public Integer getQuntity() { 
     return quntity; 
    } 

    public void setQuntity(Integer quntity) { 
     this.quntity = quntity; 
    } 
} 

這是我applicationContext.xml

<?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:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 
     http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> 

    <context:component-scan base-package="com.nought"> 
     <context:exclude-filter type="annotation" 
      expression="org.springframework.stereotype.Controller" /> 
    </context:component-scan> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 

    <bean 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
     id="emf"> 
     <property name="packagesToScan" value="com.nought.entity" /> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">create</prop> 
       <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
      </props> 
     </property> 
     <property name="persistenceProvider"> 
      <bean class="org.hibernate.jpa.HibernatePersistenceProvider" /> 
     </property> 
    </bean> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
     <property name="url" value="jdbc:mysql://localhost:3306/hibernate" /> 
     <property name="username" value="root" /> 
     <property name="password" value="" /> 
    </bean> 

    <import resource="security.xml"/> 

    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <mvc:annotation-driven /> 

    <jpa:repositories base-package="com.nought.repository" 
     entity-manager-factory-ref="emf" /> 

</beans> 

當我建立我得到這個錯誤的項目..... .......

2015-04-21 20:46:36.610:WARN:oejw.WebAppContext:main: Failed startup of context [email protected]{/,file:///C:/Users/Nought/workspace/luna/thelaststand/src/main/webapp/,STARTING}{file:///C:/Users/Nought/workspace/luna/thelaststand/src/main/webapp/} 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emf' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory 
....... 
....... 
....... 
Caused by: 
javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory 
...... 
...... 
...... 
Caused by: 
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.nought.entity.OrderProduct.pk.product in com.nought.entity.Product.orderProduct 

回答

0

我仍然不確定整體情況,但我想替換:

... "pk.orderDeatail" ... 
... "pk.product" ... 

由:

... "primaryKey.order" ... 
... "primaryKey.product" ... 

上所有出現...可以提高我們在錯誤消息的問候。 或者,您可以將primaryKey(+ getter/setter)重命名爲pk

+0

我試了你的方式。但它給出同樣的錯誤。 – Dev513

+0

..如果錯誤消息是「相同的」,那麼你沒有應用我提出的更改(因此),或者現在可能是錯誤消息:「...未知的目標實體屬性:com.nought.entity .OrderProduct.primaryKey ...「? – xerx593

+0

對不起。我也一樣,意味着它將pk更改爲primaryKey。 這裏是錯誤 'mappedBy引用一個未知的目標實體屬性:com.nought.entity.OrderProduct.primaryKey.orderDeatail in com.nought.entity.OrderDetail.orderProduct' – Dev513

相關問題