gpt4 book ai didi

java - 我是否也应该在 Java 中更新双向关系?

转载 作者:行者123 更新时间:2023-12-01 14:38:20 26 4
gpt4 key购买 nike

假设我有 UserGroup 实体,并希望它们之间具有一对多关系。 IE。每个User属于一个Group,而任何Group可以包含多个User

组实体:

@Entity
@Table(name="GroupTable")
public class Group {

@Id
@Column(name="Id")
private long id;

@Column(name="Name")
private String name;

@OneToMany(mappedBy="group")
private Set<User> users;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Set<User> getUsers() {
return users;
}

public void setUsers(Set<User> users) {
this.users = users;
}

}

和用户实体:

@Entity
@Table(name="User")
public class User {

@Id
@Column(name="Id")
private long id;

@ManyToOne
@JoinColumn(name="GroupId")
private Group group;

@Column(name="Name")
private String name;

public Group getGroup() {
return group;
}

public void setGroup(Group group) {
this.group = group;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}



}

以下是应用程序代码:

public class App_Tester_Hibernate_01 
{

private static Logger log = LoggerFactory.getLogger(App_Tester_Hibernate_01.class);

public static void main( String[] args )
{
Configuration configuration = new Configuration();
configuration.configure();


ServiceRegistry serviceRegistry;
SessionFactory sessionFactory;

serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);


Session session;

session = sessionFactory.openSession();
session.beginTransaction();

Group group;
User user;

group = (Group) session.byId(Group.class).load(1l);
if( group == null ) {

group = new Group();

group.setId(1l);
group.setName("Group #1");


session.save(group);
}

log.info("Group.Users = {}", StringUtils.collectionToCommaDelimitedString(group.getUsers()));

user = (User) session.byId(User.class).load(1l);
if( user == null ) {

user = new User();

user.setId(1l);
user.setName("Bob");
user.setGroup(group);

session.save(user);
}

log.info("Group.Users = {}", StringUtils.collectionToCommaDelimitedString(group.getUsers()));

user = (User) session.byId(User.class).load(2l);
if( user == null ) {

user = new User();

user.setId(2l);
user.setName("Alice");
user.setGroup(group);

session.save(user);
}

log.info("Group.Users = {}", StringUtils.collectionToCommaDelimitedString(group.getUsers()));

user = (User) session.byId(User.class).load(3l);
if( user == null ) {

user = new User();

user.setId(3l);
user.setName("Peter");
user.setGroup(group);

session.save(user);
}

log.info("Group.Users = {}", StringUtils.collectionToCommaDelimitedString(group.getUsers()));

session.update(group);

log.info("Group.Users = {}", StringUtils.collectionToCommaDelimitedString(group.getUsers()));

session.getTransaction().commit();
session.close();

}
}

不幸的是,getUser() 总是返回 null。为什么?不是设置为相关用户的代表吗?我做错了什么?

我应该自己进行集契约(Contract)步吗? Hibernate 不会自动填充字段吗?

更新1

我的 hibernate 配置如下:

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/tester_hibernate_01</property>
<property name="connection.username">root</property>
<property name="connection.password">sa</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>

<property name="hibernate.query.substitution">true 1, false 0, yes 'Y', no 'N'</property>

<property name="hbm2ddl.auto">create</property>

<mapping class="test.Tester_Hibernate_01.Group"/>
<mapping class="test.Tester_Hibernate_01.User"/>


</session-factory>

</hibernate-configuration>

添加

    @OneToMany(mappedBy="group", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<User> users;

没有帮助。

更新2

添加

Hibernate.initialize(group);

没有帮助,getUsers() 仍然返回 null。

最佳答案

有趣的例子:你正在使用2个事务,在你保存用户时组实体被分离,所以它不会被更新(实际上我很好奇hibernate是否可以弄清楚它是否被附加,我做不这么认为,如果你尝试一下请告诉我)。您可能想要做的是将用户添加到组实体中的集合中,然后刷新它,以便在用户实体中分配 id。然而,您需要添加的是对组实体( http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-cascade )中的集合字段的适当级联注释才能使其工作

关于java - 我是否也应该在 Java 中更新双向关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16257095/

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