简体   繁体   English

对未知实体的引用:java.lang.Integer

[英]Reference to unknown entity: java.lang.Integer

I'm having simple @Entity class 我有简单的@Entity课程

@Entity
@Table(name = "user")
@Getter       
class User {
    @Id
    @GeneratedValue
    private final Integer userID;    
    private final String username;   
}

And another one, that should hold foreign key to it 而另一个,应该掌握它的外键

@Getter
@Builder
@Entity
class Task {
    @Id
    @GeneratedValue
    private final Integer taskID;

    private final String title;
    private final String details;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinTable(name = "user", joinColumns = {@JoinColumn(name = "userID")})
    private final Integer userID;
}

The point is - User and Task classes are in separated packages and both are package scoped. 关键是 - UserTask类在分离的包中,并且都是包作用域。 Means I can't simply write 意味着我不能简单地写

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinTable(name = "user", joinColumns = {@JoinColumn(name = "userID")})
private final User user;

And thats why I'm getting 这就是为什么我得到的

@OneToOne or @ManyToOne on com.johndoe.teamplanner.task.domain.Task.userID references an unknown entity: java.lang.Integer

Is there a workaround, to achieve what I need? 是否有解决方法,以实现我的需求? Create relationship between 2 entities that are in separated packages and with package scope? 创建分离包中的两个实体与包范围之间的关系?

You could try the following alternatives: 您可以尝试以下替代方案:

  • The obvious one: change the accessor of the class User to public . 显而易见的一个:将类User的访问者更改为public But, if you asked this is probably because you couldn't change it because you don't have the source code. 但是,如果您问这可能是因为您无法更改它,因为您没有源代码。
  • Decompile the User .class file in order to get the source. 反编译User .class文件以获取源。 If User isn't so complex, you probably could try this. 如果用户不是那么复杂,你可能会试试这个。 Then, change the accessor to public . 然后,将访问者更改为public
  • Generate your own version of the User class and use yours for persistence and the other one for the functionalities it provides. 生成您自己的User类版本,并将其用于持久性,另一个用于它提供的功能。
  • Map the userID in Task class just as an Integer and assure by code that you never assign a value that isn't a valid userID: @Column private Integer userID; 地图的userIDTask类只是作为一个整数,并通过代码,你从来没有分配一个值不是一个有效的用户ID保证: @Column private Integer userID; . If you are using JPA to generate the tables, then you need to add manually the foreign key in the database to assure the integrity of your data. 如果使用JPA生成表,则需要手动添加数据库中的外键以确保数据的完整性。

I hope some of these solutions could help. 我希望其中一些解决方案可以提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM