Java实体管理器合并不编辑只创建新记录

时间:2014-03-17 17:43:16

标签: java merge entitymanager

您好我无法合并工作它只创建新记录但不更新记录

编辑:

对象:

@Entity(name = "ALLEGRO_TRANSACTION")
public class AllegroTransactionImpl implements AllegroTransaction{

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    @Column(name = "ALIEXPRESS_NUMBER")
    protected String aliexpressNumber;

    @Column(name = "CREATE_DATE")
    protected Date createDate;

    @OneToOne(optional = true, targetEntity = PaymentTypeImpl.class)
    @JoinColumn(name = "PAYMENT_ID")
    protected PaymentTypeImpl paymentType;

    @Column(name = "FIRST_NAME")
    protected String firstName;

    @Column(name = "LAST_NAME")
    protected String lastName;

    @Column(name = "PRICE")
    protected float price;

    @Column(name = "EMAIL")
    protected String email;

    @Column(name = "PHONE", nullable = true)
    protected String phone;

    @Column(name = "ADDRESS", columnDefinition="LONGTEXT")
    protected String address;

    @Column(name = "ATTENTION", columnDefinition="LONGTEXT")
    protected String attention;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAliexpressNumber() {
        return aliexpressNumber;
    }

    public void setAliexpressNumber(String aliexpressNumber){
        this.aliexpressNumber = aliexpressNumber;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public PaymentTypeImpl getPaymentType() {
        return paymentType;
    }

    public void setPaymentType(PaymentTypeImpl paymentType) {
        this.paymentType = paymentType;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAttention() {
        return attention;
    }

    public void setAttention(String attention) {
        this.attention = attention;
    }

}

管理器:

@Service
public class AllegroTransactionService {

    private final static Logger logger = Logger.getLogger(AllegroTransactionService.class);

    @PersistenceContext( unitName = "allegroTransactionPersistenceUnit", type= PersistenceContextType.EXTENDED )
    protected EntityManager em;

    public List<AllegroTransactionImpl> readAllegroTransactionByCreateDate()
    {
        Query query = this.em.createQuery( "SELECT allegroTransaction FROM com.springapp.mvc.classes.AllegroTransactionImpl allegroTransaction ORDER BY createDate DESC" );
        return query.getResultList();
    }

    @Transactional
    public AllegroTransactionImpl saveAllegroTransaction(AllegroTransactionImpl allegroTransaction)
    {
        this.em.merge( allegroTransaction );
        return allegroTransaction;
    }

}

还是不能让这个工作......也许你们想出来的东西?实体管理器工作我很容易得到readAllegroTransaction函数工作,但合并不起作用 - 它创建另一个对象。

1 个答案:

答案 0 :(得分:0)

问题未正确传递ID参数