2015-06-20 61 views
0

我在JPA是新的,我都不再按照我的项目单位:JPA persit创造了许多新的现有实体到一个关系

动物实体:

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返回的值为。

我需要使用和现有的类型创建一个新的动物,也得到最后一个ID。那可能吗?

回答

1

使用合并,你应该使用:

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

拿到参照对象,并获得其id其中还坚持并不需要,因为实体已经被托管后持续存在。

+0

谢谢阿修罗! – afym

相关问题