[英]Bidirectional Association In JPA Repository Not Working
Here are my tables: 这是我的桌子:
ORDER 订购
CREATE TABLE ORDER_DETAILS (
ID INT NOT NULL AUTO_INCREMENT
, ORDER_ID VARCHAR(60) NOT NULL
, ADDRESS_ID INT
-- , DRIVER_ID INT
, BARCODE_ID INT
, CUSTOMER_ID VARCHAR(60)
, CUSTOMER_PIN VARCHAR(60)
, UNIQUE UQ_ORDER_ID_1 (ORDER_ID)
, FOREIGN KEY(BARCODE_ID) REFERENCES public.BARCODE(ID)
, PRIMARY KEY (ID)
);
AND BARCODE 和条形码
CREATE TABLE BARCODE (
ID INT NOT NULL AUTO_INCREMENT
, BARCODE_DETAILS VARCHAR(60) NOT NULL
, DRIVER_ID INT
, VERSION INT NOT NULL DEFAULT 0
, UNIQUE UQ_BARCODE_ID_1 (BARCODE_DETAILS)
, PRIMARY KEY (ID));
Order to Barcode is Many To One Relationship ie Many Order Can Have One barcode 订单到条形码是一对多的关系,即很多订单可以有一个条形码
Here Are My repositories 这是我的存储库
OrderRepository OrderRepository
package com.eppvd.application.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.eppvd.application.domain.Address;
import com.eppvd.application.domain.Customer;
import com.eppvd.application.domain.Driver;
import com.eppvd.application.domain.Order;
public interface OrderRepository extends JpaRepository<Order, Long> {
Order findByOrderId(String orderId);
// Order findByBarcode(String barcode);
Order findByCustomerPin(String customerPin);
// Order findByDriver(Driver driver);
Order findByAddress(Address address);
Order findByCustomer(Customer customer);
}
And BarcodeRepository 和BarcodeRepository
package com.eppvd.application.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.eppvd.application.domain.Barcode;
public interface BarcodeRepository extends JpaRepository<Barcode, Long> {
Barcode findByBarcode(String barcode);
}
Here Are My ServiceImpls 这是我的服务
OrderServiceImpl OrderServiceImpl
package com.eppvd.application.service.jpa;
/** imports **/
@Service("OrderService")
@Repository
@Transactional
public class OrderServiceImpl implements OrderService {
/** Other Methods **/
@Override
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public Order save(Order order) {
return orderRepository.save(order);
}
@Override
public void flush(){
orderRepository.flush();
}
}
And BarcodeServiceImpl 和BarcodeServiceImpl
package com.eppvd.application.service.jpa;
@Service(value="barcodeService")
@Repository
@Transactional
public class BarcodeServiceImpl implements BarcodeService{
@Autowired
BarcodeRepository barcodeRepository;
@Override
@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
public Barcode save(Barcode barcode) {
return barcodeRepository.save(barcode);
}
}
And Entities 和实体
package com.eppvd.application.domain;
@Entity
@Table(name = "order_details")
public class Order implements Serializable {
private Barcode barcode;
@ManyToOne(optional = false)
@JoinColumn(name="BARCODE_ID")
public Barcode getBarcode() {
return barcode;
}
public void setBarcode(Barcode barcode) {
this.barcode = barcode;
}
}
package com.eppvd.application.domain;
@Entity
@Table(name = "barcode")
public class Barcode implements Serializable {
private static final long serialVersionUID = 3202943305655732979L;
private Long id;
private int version;
private String barcode_details;
private Set<Order> orders ;
@Column(name="BARCODE_DETAILS")
public String getBarcodeDetails() {
return barcodeDetails;
}
public void setBarcodeDetails(String barcode) {
this.barcodeDetails = barcode;
}
@OneToMany(cascade=CascadeType.ALL,mappedBy="barcode",fetch=FetchType.EAGER)
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
} }
and application Context 和应用上下文
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:schema.sql"/>
<!-- <jdbc:script location="classpath:test-data.sql"/> -->
</jdbc:embedded-database>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="packagesToScan" value="com.eppvd.application.domain"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<context:annotation-config/>
<jpa:repositories base-package="com.eppvd.application.repository"
entity-manager-factory-ref="emf"
transaction-manager-ref="transactionManager"/>
<!-- <bean id="customerService" class="com.eppvd.application.service.jpa.CustomerServiceImpl"> -->
<!-- </bean> -->
<context:component-scan base-package="com.eppvd.application.service.jpa"></context:component-scan>
</beans>
And JUNit Test 和JUNit测试
package com.eppvd.application.service;
import static org.junit.Assert.*;
@ContextConfiguration(locations="classpath:datasource-tx-jpa.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class OrderServiceTest {
@Autowired
private OrderService orderService;
@Autowired
private BarcodeService barcodeService;
@Before
public void createAddressCustomerDriver(){
Barcode barcode = new Barcode();
barcode.setBarcodeDetails("1234");
barcodeService.save(barcode);
}
@Test
public void testCreateOrder() {
Barcode barcode = barcodeService.findByBarcode("1234");
Order order = new Order();
order.setOrderId("OD001");
order.setBarcode(barcode);
order.setCustomerPin("1234");
order.setCustomer(customerService.findByFirstName("Abhijit"));
order.setAddress(addressService.findByStreet("BTM Layout"));
orderService.save(order);
orderService.flush();
Set<Order> orders = barcode.getOrders();
assertEquals("1234", orderService.findByOrderId("OD001").getBarcode().getBarcodeDetails()); /**Works **/
assertEquals(1, orders.size()); /**FAILS **/
}
}
I tried To be as detailed as possible. 我尝试了尽可能详细。 So it became a bit long.
因此它变得有点长。 Any help would be greatly appreciated.
任何帮助将不胜感激。 I tried to figure out everything logically but not too sure what am I missing.There is little documentation around this area
我试图从逻辑上找出所有内容,但不太确定我缺少什么内容。
You created an Order, and initialized its barcode. 您创建了一个订单,并初始化了其条形码。 But you never added the order to the set of orders of the barcode.
但是您从未将订单添加到条形码的订单集中。 So its set of barcodes is empty.
因此其条形码集为空。 That's expected.
那是意料之中的。
You're supposed to maintain the two sides of an association if you expect them to be coherent. 如果您希望它们的连贯性,则应该维护它们的两个方面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.