Json 坚持使用AngularJS和JPA

Json 坚持使用AngularJS和JPA,json,angularjs,jpa,persistence,Json,Angularjs,Jpa,Persistence,我正在尝试使用GoogleGuice、JPA(EclipseLink)和AngularJS开发一个应用程序。 基本上,在这个应用程序中,我有一些员工,每个人都有一份以上的薪水 public class Employee implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="ID_EMPLOYEE") private long idEmployee; @OneToMan

我正在尝试使用GoogleGuice、JPA(EclipseLink)和AngularJS开发一个应用程序。 基本上,在这个应用程序中,我有一些员工,每个人都有一份以上的薪水

public class Employee implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID_EMPLOYEE")
private long idEmployee;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "employee",targetEntity = Salary.class,fetch = FetchType.EAGER)
private List<Salary> salaries;
现在,我可以使用angularJS和REST服务毫无问题地插入一名新员工。我不能做的是给现有员工增加新的工资。 基本上,我在angularJS控制器中拥有的是:

$scope.employee.salaries.push(this.salaryToAdd);
employeeServ.persistSalary(employee);
然后我就救了那个雇员。工资是节省下来的,但当我试图得到员工时,有时我能看到工资,有时却看不到。我注意到的是,工资与员工没有任何关系,所以当我得到员工时,JPA可能不知道工资是否与该员工有关

这是我的dao和与持久性相关的部分:

public abstract class GenericDao<E> implements AbstractDao<E> {

@Inject
private EntityManager em;

protected abstract Class<E> getGenericClass();

@Transactional
@Override
public void insert(E entity) {
    em.persist(entity);
}

@Transactional
@Override
public void update(E entity) {
    em.merge(entity);
}

@Transactional
@Override
public void delete(E entity) {
    em.remove(entity);
}

public E findById(Long id) {
    Class<E> clazz = getGenericClass();
    return em.find(clazz, id);
}

public List<E> findAll() {
    Class<E> clazz = getGenericClass();
    CriteriaQuery<E> query = em.getCriteriaBuilder().createQuery(clazz);
    Root<E> root = query.from(clazz);
    query.select(root);
    return em.createQuery(query).getResultList();
}

public EntityManager getEntityManager() {
    return em;
}



@Path("/employee")

您的JPA映射看起来是正确的,您不需要
工资
中的
员工
参考。这只会使它成为一种与你现在的单向关系相反的关系。你能添加保存的相关代码部分并读取
Employee
实体,然后序列化为JSON吗?谢谢你的回答,我编辑了我的Post。你能看看错误是来自JSON反序列化程序还是来自JPA合并方法吗?(调试post方法并查看json反序列化后实体是否正确)我进行了调试,但没有注意到json反序列化器上的任何问题。问题出在其他地方,因为反序列化的实体实际上是dao检索到的实体。我想知道,我是否需要保留工资,然后将其添加到员工中?
public abstract class GenericDao<E> implements AbstractDao<E> {

@Inject
private EntityManager em;

protected abstract Class<E> getGenericClass();

@Transactional
@Override
public void insert(E entity) {
    em.persist(entity);
}

@Transactional
@Override
public void update(E entity) {
    em.merge(entity);
}

@Transactional
@Override
public void delete(E entity) {
    em.remove(entity);
}

public E findById(Long id) {
    Class<E> clazz = getGenericClass();
    return em.find(clazz, id);
}

public List<E> findAll() {
    Class<E> clazz = getGenericClass();
    CriteriaQuery<E> query = em.getCriteriaBuilder().createQuery(clazz);
    Root<E> root = query.from(clazz);
    query.select(root);
    return em.createQuery(query).getResultList();
}

public EntityManager getEntityManager() {
    return em;
}



@Path("/employee")
@Inject
EmployeeDao dao;

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/get/{id}")
public String get(@PathParam("id") String id) {
    final Gson gson = new GsonBuilder().setPrettyPrinting().create();
    if("all".equals(id)) {
        return gson.toJson(dao.findAll().toArray());
    } else {
        return gson.toJson(dao.findById(Long.valueOf(id)));
    }
}

@POST
@Path("/post")
public void post(String employee) {
    final Gson gson = new GsonBuilder().setPrettyPrinting().create();
    Employee entity = gson.fromJson(employee, Employee.class);
    dao.update(entity);
}