gpt4 book ai didi

java - 仅从 @ManyToOne 关系中获取一列

转载 作者:行者123 更新时间:2023-11-30 05:48:23 26 4
gpt4 key购买 nike

我正在尝试缩小我的实体之一,因为它不需要加载整个相关对象。我想要实现的不是加载映射为 @ManyToOne 关系的整个实体,而是仅加载其字段之一。假设我们有以下两个实体(来自样本的实体是组成的):

@Entity
public class Session {
@Id
private Long id;
private String someField;
@ManyToOne
@JoinColumn(name = "sessionId", referencedColumnName = "id")
private User user;
}

@Entity
public class User {
@Id
private Long id;
private String username;
private String address;
private LocalDateTime lastLogin;
}

我希望最终得到较小的 session 实体,如下所示:

@Entity
public class Session {
@Id
private Long id;
private String someField;
@ManyToOne
@JoinColumn(name = "sessionId", referencedColumnName = "id")
private LocalDateTime lastLogin; // lastLogin value from User Entity
}

我试图通过混合@Column注释来实现这一点,但忘记了它在@ManyToOne上是不允许的。我正在考虑可以使用 @ElementCollection 完成类似的事情:

    @ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "OTHER_TABLE")
@Column(name = "someFieldFromOtherTable")
private Set<String> someFieldFromOtherTableValues;

是否有可能从反射实体中仅获取一列?

最佳答案

这就是我的代码中的内容。尝试一下:

@Entity
@Table(name = "t_address")
@SecondaryTables({
@SecondaryTable(name="t_city"),
@SecondaryTable(name="t_country")
})
public class FirstTable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String street1;
private String street2;

@Column(table="t_city")
private String city;
@Column(table="t_city")
private String state;
@Column(table="t_city")
private String zipcode;

@Column(table="t_country")
private String country;
}

关于java - 仅从 @ManyToOne 关系中获取一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54426249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com