我遇到了JPA问题。当我尝试持久保存新实体时,我会收到以下错误:
java.lang.IllegalArgumentException:Object:技术不是已知的 实体类型。
技术是Category类的标题。 (注意:我的代码以前工作过!)所以我决定删除数据库中的所有表(PostgreSql)
N̶o̶w̶̶I̶̶h̶a̶v̶e̶̶t̶h̶e̶̶f̶o̶l̶l̶o̶w̶i̶n̶g̶̶e̶r̶r̶o̶r̶:̶
(我之前有过这个错误,但过了一会儿就消失了)
javax.persistence.PersistenceException:Exception [EclipseLink-4002](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.DatabaseException
内部异常:org.postgresql.util.PSQLException:错误:关系“类别”不存在
位置:47
错误代码:0
调用:SELECT ID,DESCRIPTION,TITLE,PARENT_ID FROM category
查询:ReadAllQuery(referenceClass = Category sql =“SELECT ID,DESCRIPTION,TITLE,PARENT_ID FROM category”)
实体类(Category.Java)
@Entity
public class Category implements DataObject, Serializable {
@Id
@GeneratedValue
private Long id;
private String title;
private String description;
@ManyToOne @JsonIgnore
private Category parent;
@Transient
private Long parentId;
//functions
}
的persistence.xml
<persistence-unit name="BloggerPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>blogdb</jta-data-source>
<class>domain.Blog</class>
<class>domain.BlogUser</class>
<class>domain.Category</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
修改
我通过重启glassfish服务器解决了ERROR关系。 现在我回到第一个错误。
DatabaseJPA.java
public class DatabaseJPA<T extends DataObject> implements Database<T> {
private EntityManagerFactory factory;
private EntityManager manager;
private Class<T> type;
public DatabaseJPA(String name, Class<T> type)
{
factory = Persistence.createEntityManagerFactory(name);
manager = factory.createEntityManager();
this.type = type;
}
@Override
public T ReadValue(long id) {
return (T) manager.find(type, id);
}
@Override
public void InsertValue(T value) {
manager.getTransaction().begin();
manager.persist(value);
manager.flush();
manager.getTransaction().commit();
}
@Override
public void UpdateValue(long id, T value) {
manager.getTransaction().begin();
manager.merge(value);
manager.flush();
manager.getTransaction().commit();
}
@Override
public void DeleteValue(long id) {
manager.getTransaction().begin();
manager.remove(this.ReadValue(id));
manager.flush();
manager.getTransaction().commit();
}
@Override
public void close() throws IOException {
factory.close();
manager.close();
}
}
答案 0 :(得分:0)
如果您再次遇到关系问题:
@ManyToOne
private A parent;
@OneToMany(mappedBy="parent")
private Collection<A> children;
对于IllegalArgumentException,您可以向我们展示一部分代码,您可以在其中调用持久性并引发异常吗?
答案 1 :(得分:0)
将persistence.xml中的transaction-type
更改为LOCAL_RESOURCE
解决了问题并重新启动了glassfish服务器。