我想将方法添加到我的用户实体,该实体返回其他实体的列表,该列表是用户实体中的字段但已过滤。问题是,当我将这样的方法添加到实体类时,我得到错误
[class entities.WorksOnTask]在关系属性[field user]中使用非实体[class entities.User]作为目标实体
为什么,如何添加返回(过滤)WorksOnTask列表的方法i User实体类?
或者我还有其他什么需要做什么?
@Entity
@Table(name = "user")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
@NamedQuery(name = "User.findByUsername", query = "SELECT u FROM User u WHERE u.username = :username"),
@NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password"),
@NamedQuery(name = "User.findByFirstName", query = "SELECT u FROM User u WHERE u.firstName = :firstName"),
@NamedQuery(name = "User.findByLastName", query = "SELECT u FROM User u WHERE u.lastName = :lastName"),
@NamedQuery(name = "User.findByAppPrivileges", query = "SELECT u FROM User u WHERE u.appPrivileges = :appPrivileges"),
@NamedQuery(name = "User.findByActivated", query = "SELECT u FROM User u WHERE u.activated = :activated")})
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "Username")
private String username;
@Basic(optional = false)
@Column(name = "Password")
private String password;
@Basic(optional = false)
@Column(name = "FirstName")
private String firstName;
@Basic(optional = false)
@Column(name = "LastName")
private String lastName;
@Basic(optional = false)
@Column(name = "AppPrivileges")
private int appPrivileges;
@Basic(optional = false)
@Column(name = "Activated")
private boolean activated;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.LAZY)
private List<WorksOnProject> worksOnProjectList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.LAZY)
private List<WorksOnTask> worksOnTaskList;
public List<WorksOnTask> getWorksOnTaskList(){
return worksOnTaskList;
}
public void setWorksOnTaskList(List<WorksOnTask> worksOnTaskList) {
this.worksOnTaskList = worksOnTaskList;
}
public List<WorksOnTask> findWorksOnTaskListNonRemoved(){
return new ArrayList(worksOnTaskList).stream().filter((w)->!w.getRemoved()).collect(Collectors.toList());
}
堆栈跟踪显示实体任务的错误,但一切都与用户相同。
Exception in Application start method
java.lang.reflect.InvocationTargetException at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483) at
com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at
com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483) at
sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start
method at
com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at
com.sun.javafx.application.LauncherImpl$$Lambda$53/200006406.run(Unknown
Source) at java.lang.Thread.run(Thread.java:745) Caused by: Exception
[EclipseLink-30005] (Eclipse Persistence Services -
2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for
persistence archives with ClassLoader:
sun.misc.Launcher$AppClassLoader@55f96302 Internal Exception:
javax.persistence.PersistenceException: Exception [EclipseLink-28018]
(Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):
org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [DBPU] failed.
Internal Exception: Exception [EclipseLink-7250] (Eclipse Persistence
Services - 2.5.2.v20140319-9ad6abd):
org.eclipse.persistence.exceptions.ValidationException Exception
Description: [class entities.Project] uses a non-entity [class
entities.Task] as target entity in the relationship attribute [field
iDRootTask]. at
org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127)
at
org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:107)
at
org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
at
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at pg.jpa.JpaAccessManager.<init>(JpaAccessManager.java:24) at
pg.Login.start(Login.java:49) at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at
com.sun.javafx.application.LauncherImpl$$Lambda$56/574011920.run(Unknown
Source) at
com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at
com.sun.javafx.application.PlatformImpl$$Lambda$50/1768305536.run(Unknown
Source) at
com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at
com.sun.javafx.application.PlatformImpl$$Lambda$52/1148099840.run(Unknown
Source) at java.security.AccessController.doPrivileged(Native Method)
at
com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at
com.sun.javafx.application.PlatformImpl$$Lambda$51/1146743572.run(Unknown
Source) at
com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at
com.sun.glass.ui.gtk.GtkApplication.lambda$null$45(GtkApplication.java:126)
at
com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/379110473.run(Unknown
Source) ... 1 more Caused by: javax.persistence.PersistenceException:
Exception [EclipseLink-28018] (Eclipse Persistence Services -
2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [DBPU] failed.
Internal Exception: Exception [EclipseLink-7250] (Eclipse Persistence
Services - 2.5.2.v20140319-9ad6abd):
org.eclipse.persistence.exceptions.ValidationException Exception
Description: [class entities.Project] uses a non-entity [class
entities.Task] as target entity in the relationship attribute [field
iDRootTask]. at
org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenceException(EntityManagerSetupImpl.java:1954)
at
org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1945)
at
org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:98)
at
org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:96)
... 19 more Caused by: Exception [EclipseLink-28018] (Eclipse
Persistence Services - 2.5.2.v20140319-9ad6abd):
org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [DBPU] failed.
Internal Exception: Exception [EclipseLink-7250] (Eclipse Persistence
Services - 2.5.2.v20140319-9ad6abd):
org.eclipse.persistence.exceptions.ValidationException Exception
Description: [class pentities.Project] uses a non-entity [class
entities.Task] as target entity in the relationship attribute [field
iDRootTask]. at
org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:230)
... 23 more Caused by: Exception [EclipseLink-7250] (Eclipse
Persistence Services - 2.5.2.v20140319-9ad6abd):
org.eclipse.persistence.exceptions.ValidationException Exception
Description: [class entities.Project] uses a non-entity [class
entities.Task] as target entity in the relationship attribute [field
iDRootTask]. at
org.eclipse.persistence.exceptions.ValidationException.nonEntityTargetInRelationship(ValidationException.java:1378)
at
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.RelationshipAccessor.getReferenceDescriptor(RelationshipAccessor.java:553)
at
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ObjectAccessor.processForeignKeyRelationship(ObjectAccessor.java:659)
at
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ObjectAccessor.processOwningMappingKeys(ObjectAccessor.java:733)
at
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ManyToOneAccessor.process(ManyToOneAccessor.java:123)
at
org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processOwningRelationshipAccessors(MetadataProject.java:1580)
at
org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage3(MetadataProject.java:1833)
at
org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:580)
at
org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:585)
at
org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1869)
... 21 more Exception running application pg.Login
答案 0 :(得分:4)
我确实找到了这个问题的答案。问题是,似乎我无法在该方法中访问实体类的映射字段。但我使用查询来获取我想要的信息。
所以我没有问题了。但仍有疑问,寻找答案。
编辑: 好吧,似乎在属于实体类的方法中使用lambda表达式会产生问题。
例如,这不起作用:
public List<WorksOnTask> getWorksOnTaskListNonRemoved() {
ArrayList<WorksOnTask> wots = new ArrayList<>(worksOnTaskList);
wots.removeIf((w)->w.getRemoved());
return wots;
}
但是这样做(或者至少应用程序启动没有错误):
public List<WorksOnTask> getWorksOnTaskListNonRemoved() {
ArrayList<WorksOnTask> wots = new ArrayList<>(worksOnTaskList);
wots.removeIf(new Predicate<WorksOnTask>(){
@Override
public boolean test(WorksOnTask t) {
return t.getRemoved();
}
});
return wots;
}
这不是Java 8流和lambdas不能很好地协同工作的唯一情况。 流不适用于实体的间接列表。所以当我一起使用它们时,我需要写下这样的东西:
new ArrayList<>(user.getWorksOnTaskList()).stream().....
而不是:
user.getWorksOnTaskList().stream()......
太糟糕了,我喜欢溪流比我更喜欢JPA。
答案 1 :(得分:1)
它在EclipseLink 2.6.0 https://bugs.eclipse.org/bugs/show_bug.cgi?id=429992
中解决