我有2个实体User和Profile ,其中一个用户有一个配置文件。
User实体映射非常清晰,如下所示:
@Entity public class User { @Id private String id; @Column(unique = true, nullable = false) private String email; @Column(unique = true, nullable = false) private String name; }所以问题是关于Profile实体映射,这里的棘手问题是Profile包含用户的电子邮件(不是整个用户的实体),但它不应该由Profile更新或存储,因此该电子邮件是来自外部用户实体的readonly属性。
我使用以下Profile的实体映射来获取用户的电子邮件:
@Entity public class Profile { @Id private String userId; @PrimaryKeyJoinColumn private User user; @Basic private String firstName; @Basic private String lastName; // ... public String getEmail() { return user.getEmail(); } }所以我决定加入整个实体并将工作委托给它。 据我所知,不可能像@Column一样使用@JoinColumn :
@OneToOne @JoinColumn(name = "userId", insertable = false, updatable = false) @Column(name = "email") private String email;我也不确定使用@SecondaryTable因为它似乎是为了不同的目的而设计的。
有没有更好的方法来获取使用JPA映射的外国实体字段 ?
JPA后端:EclipseLink 2.6.2I have 2 entities User and Profile where one user has one profile.
The User entity mapping is pretty clear and looks like this:
@Entity public class User { @Id private String id; @Column(unique = true, nullable = false) private String email; @Column(unique = true, nullable = false) private String name; }So the question is about Profile entity mapping, the tricky thing here is that Profile includes user's email(not entire user's entity), but it shouldn't be either updated or stored by Profile, so the email is readonly attribute from the foreign User entity.
I used the following Profile's entity mapping for getting User's email:
@Entity public class Profile { @Id private String userId; @PrimaryKeyJoinColumn private User user; @Basic private String firstName; @Basic private String lastName; // ... public String getEmail() { return user.getEmail(); } }So i decided to join the entire entity and delegate the work to it. As far as i understand it is impossible to use @JoinColumn in couple with @Column like this:
@OneToOne @JoinColumn(name = "userId", insertable = false, updatable = false) @Column(name = "email") private String email;I am also not sure about using of @SecondaryTable as it seems that it is designed for a different purpose.
Is there any better approach for getting foreign entity field using JPA mappings?
JPA Backend: EclipseLink 2.6.2最满意答案
这不是JPA的目的。 通过调用user.getEmail()来获取电子邮件是您最干净的选择。
你不应该担心加载整个用户; 我认为这是一个单一的连接,JPA应该为你做。 性能影响应该是最小的。 (您可以简单地将内部用户对象暴露给不会过多地影响您的对象设计。使用JPA时,您总是限制您的OO设计选项)。
如果您使用的是休眠,那么故事会有所不同。 然后你可以使用@Formula注释。 但它不会更高效。 Eclipselink并不喜欢它。
That's not really what JPA was designed to do. Getting the email by just calling user.getEmail() is the cleanest option you have.
You shouldn't be worried too much about loading the entire user; the way I see it it's a single join, and JPA should do it for you. The performance impact should be minimal. (you can simple not expose the internal user object to not impact your object design too much. When using JPA, you're always limiting your OO design options though).
If you were using hibernate, the story would be different. Then you could use the @Formula annotation. It would not be more performant though. Eclipselink has nothing like it.
更多推荐
发布评论