0

假设我有UserGroup实体并希望它们之间有一对多的关系。即每个都User属于一个Group,而anyGroup可以包含许多Users。

集团实体:

@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。为什么?不是设置为相关用户的代表吗?我做错了什么?

我应该自己做集合同步吗?Hibernate 不会自动填充字段吗?

更新 1

我的休眠配置如下:

<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。

4

1 回答 1

1

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

于 2013-04-28T20:17:41.100 回答