gpt4 book ai didi

java - OneToMany 关系不加载集合

转载 作者:行者123 更新时间:2023-12-01 12:12:11 25 4
gpt4 key购买 nike

我有一个与 TravellerVote 存在关系的类 Traveller。我希望检索旅行者发送的选票和收到的选票。

等级旅行者

@Entity
@PrimaryKeyJoinColumn(name="user_id")
public class Traveller extends User{

@OneToMany(mappedBy="voter", cascade={CascadeType.PERSIST, CascadeType.REMOVE})
protected List<TravellerVote> myVotes;

@OneToMany(mappedBy="partner", cascade={CascadeType.PERSIST, CascadeType.REMOVE})
protected List<TravellerVote> receivedVotes;

public Traveller(){
super();
myVotes = new ArrayList<TravellerVote>();
receivedVotes = new ArrayList<TravellerVote>();
}

public void addVote(TravellerVote vote){
myVotes.add(vote);
}
}

旅行者投票类别

@Entity
public class TravellerVote {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(length=11)
protected long id;

@ManyToOne
@JoinColumn(name="voter_id")
protected Traveller voter;

@ManyToOne
@JoinColumn(name="partner_id")
protected Traveller partner;

@Column(nullable=false)
protected Date date;

@Column(length=300, nullable=true)
protected String comment;

@Column(nullable=false)

protected boolean positive;
public TravellerVote(Traveller voter, Traveller partner, String comment, boolean positive){
this.date = new Date();
this.voter = voter;
this.partner = partner;
this.comment = comment;
this.positive = positive;
}
}

实体正确地保存在数据库中,如下:

TravellerDao dao = new TravellerDao();
Traveller t1 = dao.findByUsername("ramiromd");
Traveller t2 = dao.findByUsername("danimd");
Traveller t3 = dao.findByUsername("famartinezd");

t1.addVote(new TravellerVote(t1, t2, "Test comment", true));
t2.addVote(new TravellerVote(t2, t1, "Test comment", true));
t3.addVote(new TravellerVote(t3, t1, "Test comment", false));

dao.save(t1);
dao.save(t2);
dao.save(t3);

TravellerVote after save entities

但是,当在其他 Controller 中尝试获取投票时,集合有 0 个元素。

TravellerDao dao = new TravellerDao();
Traveller t1 = dao.findByUsername("ramiromd");
t1.getReceivedVotes().size(); // Must be 2 not 0.
t1.getMyVotes().size(); // Must be 1 not 0.

TravelerDao 保存方法:

public void save(Traveller traveller) {
EntityManager em = PersistenceHelper.getEm();
em.getTransaction().begin();
em.persist(traveller);
//em.flush();
em.getTransaction().commit();
}

这是findByUsername方法:

public Traveller findByUsername(String username){

EntityManager em = PersistenceHelper.getEm();
String qstring = "SELECT t FROM Traveller t WHERE t.username LIKE :username";
TypedQuery<Traveller> query = em.createQuery(qstring, Traveller.class);
query.setParameter("username", username);


return query.getSingleResult();
}

有什么想法吗?

最佳答案

在这些场景中,启用实现框架的调试会很有帮助。例如,如果您使用 Hibernate,请将 org.hibernate 包的调试级别设置为“DEBUG”。这将显示所有被触发的 SQL。在您的问题中,如果您将 fetch 设置为 LAZY ,您将看到这些语句触发了精确的 SQL:-

t1.getReceivedVotes().size(); // Must be 2 not 0.
t1.getMyVotes().size();

希望这能给您足够的提示来解决这个问题。

关于java - OneToMany 关系不加载集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27205811/

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