JPA persit以多对一的关系创建新的现有实体

时间:2015-06-20 21:32:36

标签: java hibernate jpa insert

我是JPA的新手,我在项目中遵循实体:

动物实体:

package com.shop.model;

import java.io.Serializable;

import javax.persistence.*;

@Entity
@NamedQueries({
    @NamedQuery(name="Animal.findAll", query="SELECT a FROM Animal a"),
    @NamedQuery(name="Animal.findAllByTypeId", query="SELECT a FROM Animal a WHERE a.type.id = :id"),
})
public class Animal implements Serializable {
    private static final long serialVersionUID = 1L;
    public static String SMALL = "small";
    public static String MEDIUM = "medium";
    public static String LARGE = "large";

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Lob
    private String description;

    private String name;

    private String size;

    @ManyToOne(cascade=CascadeType.PERSIST)
    private Type type;

    public Animal() {
    }

    public int getId() {
        return this.id;
    }

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

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSize() {
        return this.size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public Type getType() {
        return this.type;
    }

    public void setType(Type type) {
        this.type = type;
    }

}

输入实体:

package com.shop.model;

import java.io.Serializable;
import javax.persistence.*;
import java.sql.Timestamp;
import java.util.List;

@Entity
@NamedQueries({
    @NamedQuery(name="Type.findAll", query="SELECT t FROM Type t"),
})
public class Type implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private Timestamp created;

    private String name;

    @OneToMany(mappedBy="type", cascade=CascadeType.PERSIST)
    private List<Animal> animals;

    public Type() {
    }

    public int getId() {
        return this.id;
    }

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

    public Timestamp getCreated() {
        return this.created;
    }

    public void setCreated(Timestamp created) {
        this.created = created;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Animal> getAnimals() {
        return this.animals;
    }

    public void setAnimals(List<Animal> animals) {
        this.animals = animals;
    }

    public Animal addAnimal(Animal animal) {
        getAnimals().add(animal);
        animal.setType(this);

        return animal;
    }

    public Animal removeAnimal(Animal animal) {
        getAnimals().remove(animal);
        animal.setType(null);

        return animal;
    }
}

我正在创建新类型:

Type type = new Type();
type.setName("Some new animal type");
this.entityManager.getTransaction().begin();
this.entityManager.persit(type);
this.entityManager.getTransaction().commit();
int lastInsertId = type.getId(); // wotks great!

然而,当我尝试使用现有类型持久化新动物并且JPA创建时 新型。这是示例

Animal animal = new Animal();
Type type = this.entityManager.getReference(Type.class, 9);

animal.setName(name);
animal.setDescription(description);
animal.setSize(size);
animal.setType(type);

this.entityManager.getTransaction().begin();
this.entityManager.persit(animal);
this.entityManager.getTransaction().commit();
int lastId = animal.getId(); // works great !

它工作正常(创建了一个新动物),但它也创建了一个新类型,这就是问题所在。我还尝试了 find(Object object,int id)方法,但它仍然在创建一个新的Type。

我尝试使用合并方法而不是persit,它运行正常!但是,getId返回 0 值。

我需要使用现有的Type创建一个新动物,并获得最后一个id。这可能吗?

1 个答案:

答案 0 :(得分:1)

使用合并时,您应该使用:

    Entity entity=entityManager.merge(newEntity);
    int lastId=entity.getId();

获取对象的引用并获取其持久化不需要的id,因为实体在持久化后已经被管理。