0

我很困惑,我试图找出为什么我在数据库中使用以下代码创建了两个客户,但没有运气。我试图消除代码中的所有噪音,我希望我没有删除任何对解决问题很重要的东西。这里的顺序如下:实体、DAO、服务和具有包含虚假行为的specialTreatmentSpecialService 。

specialTreatment中,目标是获取一个没有客户链接的现有订单,创建一个新客户,并将其与订单相关联。

实体:

订单.java:

@Entity
public class Order {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "pk_id_order")
    private Integer id;

    // Other fields ...

    @BatchFetch(value = BatchFetchType.IN)
    @JoinColumn(name = "fk_id_customer", referencedColumnName = "pk_id_customer")
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Customer customer;

    // Other fields ...

    // Getter & setters for each field

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 79 * hash + (this.id != null ? this.id.hashCode() : 0);

        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof Order)) {
            return false;
        }

        final Order other = (Order) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }

        return true;
    }
}

客户.java:

@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "pk_id_customer")
    private Integer id;

    // Other fields

    @OrderBy(value = "createdAt DESC")
    @OneToMany(mappedBy = "customer", fetch = FetchType.LAZY)
    private List<Order> orders;

    @Override
    public int hashCode() {
        return (id != null ? id.hashCode() : 0);
    }

    @Override
    public boolean equals(Object object) {
        if (object == null || !(object instanceof Customer)) {
            return false;
        }

        final Customer other = (Customer) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }

        return true;
    }
}

DAO:

@Repository
@Transactional
public class CustomerDao {

    @PersistenceContext
    protected EntityManager em;

    public void create(Customer customer) {
        this.em.persist(customer);
    }
}

@Repository
@Transactional
public class OrderDao {

    @PersistenceContext
    protected EntityManager em;

    public Order edit(Order order) {
        return this.em.merge(order);
    }
}

服务 :

@Service
@Transactional
public class CustomerService {

    @Autowired
    private CustomerDao customerDao;

    public void create(Customer customer) {
        this.customerDao.create(customer);
    }
}

@Service
@Transactional
public class OrderService {

    @Autowired
    private OrderDao orderDao;

    public void edit(Order order) {
        this.orderDao.edit(order);
    }
}

@Service
@Transactional
public class SpecialService {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private OrderService orderService;

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void specialTreatment(Order order) {
        Customer customer = new Customer();
        // Fill customer ...

        this.customerService.create(customer); // LINE X

        order.setCustomer(customer); // LINE Y

        this.orderService.edit(order);
    }
}

注意:当评论第 X 行时:-没有创建客户(如预期),订单按预期编辑当评论第 Y 行时:-按预期创建客户(只有一行)但未链接到我的订单

从 Spring MVC 控制器调用代码

有什么建议吗?

4

0 回答 0