Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 重复创建JPA实体_Spring_Jpa_Eclipselink_Transactional - Fatal编程技术网

Spring 重复创建JPA实体

Spring 重复创建JPA实体,spring,jpa,eclipselink,transactional,Spring,Jpa,Eclipselink,Transactional,我很困惑,我试图找出为什么我用下面的代码在数据库中创建了两个客户,但运气不好。我试图从代码中去除所有噪音,我希望我没有删除任何对解决问题重要的内容。 以下是按以下顺序排列的:实体、DAO、服务和带有保留虚假行为的specialTreatment的SpecialService 在specialTreatment中,目标是获得一个没有客户链接的现有订单,创建一个新客户,并将其与订单关联 实体: Order.java: @Entity public class Order { @Id

我很困惑,我试图找出为什么我用下面的代码在数据库中创建了两个客户,但运气不好。我试图从代码中去除所有噪音,我希望我没有删除任何对解决问题重要的内容。 以下是按以下顺序排列的:实体、DAO、服务和带有保留虚假行为的specialTreatment的SpecialService

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

实体:

Order.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;
    }
}
Customer.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;
    }
}
服务:

@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控制器调用的


有什么建议吗?

您是否在实体中实现了正确的equals/hashcode实现?!接下来,在您的
订单
实体中没有级联,它是双向的还是单向的关系?只是用客户和订单的equals/hashcode(由Netbeans生成)编辑了我的问题。它们基于自动递增的ID,此时未为客户设置该ID。我还添加了一个从客户到订单的关系,我在这里没有使用。您应该始终设置关系的双方(根据我的经验,如果您不这样做,Hibernate可能会失去跟踪)。我尝试将订单添加到客户的订单列表中,然后继续执行任何操作,但出现相同的问题。我真的很困惑!是否可以将对象序列化到CustomerService和OrderService?如果是这样,则显示为您调用persist的客户实例与与订单关联的客户实例不同。尝试在CustomerService create方法中调用em.flush()以分配主键,并检查该值是否在specialTreatment中的create调用后设置。
@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);
    }
}