我有两个实体患者和文件: Patients.java:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "personId", nullable = false, unique = true)
private Long personId;
@OneToMany(fetch = FetchType.EAGER,cascade= CascadeType.ALL)
@JoinTable(name="File_Patient")
private List<Files> files = new ArrayList<>();
Files.java:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "personId", nullable = false, unique = true)
private Long personId;
@Column(name = "fileName", nullable = true, unique = false)
private String fileName;
我正在使用json添加数据。添加很顺利。我这样添加:
"files": [
{
"personId": 3,
"fileName": "1A"
},
{
"personId": 4,
"fileName": "2B"
},
{
"personId": 5,
"fileName": "3C"
},
{
"personId": 6,
"fileName": "4D"
}
]
我的更新dao中的Person方法:
public Patients updatePerson(String person) {
Patients result = new Patients();
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse(person);
try {
Long personId = (Long) obj.get("personId");
em = factory.createEntityManager();
String queryStr = "SELECT c FROM Patients c where c.personId = '" + personId + "'";
TypedQuery<Patients> query = em.createQuery(queryStr, Patients.class);
result = query.getSingleResult();
List<Files> f = result.getFiles();
if (obj.get("files") != null) {
boolean flag;
JSONArray files = (JSONArray) obj.get("files");
List<Files> fi = new ArrayList<>();
Files file = new Files();
for (int i = 0 ; i < files.size() ; i ++) {
log.info(files.get(i).toString());
file.setFileName(files.get(i).toString());
fi.add(file);
}
result.setFiles(fi);
}
em.getTransaction().begin();
em.merge(result);
em.getTransaction().commit();
}
} catch (Exception e) {
log.error("Exception caught :: " + e);
em.getTransaction().rollback();
}
em.close();
return result;
}
但情况是: 在更新时,我会添加更多文件。代码需要检查并删除未更新的文件并添加新文件。 现在,我无法使用通常的em.remove()删除。
虽然添加它会出现此错误: 异常捕获:: java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:File_patient未映射[SELECT f FROM File_patient f其中f.files_personid =&#39; 3&#39;]
知道如何继续吗?