JPA持续两次

时间:2015-08-18 10:11:04

标签: java mysql jpa netbeans

我正在尝试将一个看起来像这样的实体保存到MySQL数据库中:

@Entity
@Table(catalog = "logistic", schema = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Movement.findAll", query = "SELECT m FROM Movement m"),
    @NamedQuery(name = "Movement.findByIdMovement", query = "SELECT m FROM Movement m WHERE m.idMovement = :idMovement"),
    @NamedQuery(name = "Movement.findByQty", query = "SELECT m FROM Movement m WHERE m.qty = :qty"),
    @NamedQuery(name = "Movement.findByDt", query = "SELECT m FROM Movement m WHERE m.dt = :dt")})
public class Movement implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id_movement", nullable = false)
    private Long idMovement;
    @Basic(optional = false)
    @NotNull
    @Column(nullable = false)
    private int qty;
    @Basic(optional = false)
    @Column(nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date dt;
    @JoinColumn(name = "from_id_container", referencedColumnName = "id_container", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Container fromIdContainer;
    @JoinColumn(name = "to_id_container", referencedColumnName = "id_container", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Container toIdContainer;
    @JoinColumn(name = "id_product", referencedColumnName = "id_product", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Product idProduct;
    @JoinColumn(name = "username", referencedColumnName = "username", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private User username;
// Getters and setters...

此实体由netbeans生成。问题是,当我尝试将数据持久存储到数据库中时,它会插入两个文件而不是一个。我用来持久保存数据的代码是

Movement movement = new com.pccomponentes.sga.entity.Movement();
movement.setFromIdContainer(from);
movement.setToIdContainer(to);
movement.setIdProduct(product);
movement.setQty(obj.getInt("qty"));
movement.setUsername(userEJB.find(redis.get("token-" + token)));
movementEJB.create(movement);

发生了什么事?

提前谢谢!

0 个答案:

没有答案