Hibernate多对多关系只能以一种方式显示

时间:2015-05-12 14:35:24

标签: java hibernate jpa playframework

Java 8,Hibernate 4.3.8.Final,Play Framework,使用官方驱动程序的SQL Server,使用IntelliJ 14.1.2。

基本上,我有一组用户和一组AccessGroup。组拥有用户,用户拥有组,多对多。

Persistent.java

package models;

import play.db.jpa.JPA;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;


@MappedSuperclass
public class Persistent implements Serializable{

protected Persistent() {
    this.creationTime = new Timestamp(System.currentTimeMillis());
    this.modificationTime = new Timestamp(System.currentTimeMillis());
}

protected final static String DATE_FORMAT = "MM/dd/yyyy hh:mm a";

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

... etc(其他getter / setter,persist(),update(),remove()方法等)

AccessGroup.java:

package models;

import javax.persistence.*;
import java.util.Collection;
import java.util.Set;

@Entity
@AttributeOverrides({@AttributeOverride(name = "id", column = @Column(name = "accessGroupId"))})
public class AccessGroup extends Persistent {

    @ManyToMany(        
        cascade = {CascadeType.PERSIST, CascadeType.MERGE}, 
        mappedBy = "accessGroups", 
        targetEntity = User.class)
    private Collection<User> users;

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

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

......等(其他不参与此关系的吸气剂和制定者)

User.java:     包模型;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.util.Collection;



@Entity
@Table(name = "AppUsers")
@AttributeOverrides({@AttributeOverride(name = "id", column = @Column(name = "userId"))})
public class User extends Persistent{
    public User() {};

    public static UserFinder getFinder() {
        return new UserFinder();
    }

    public static class UserFinder extends Finder<User> {

        public UserFinder () {
            super(User.class);
        }

}
    @ManyToMany(fetch = FetchType.EAGER, 
            targetEntity = AccessGroup.class,        
            cascade={CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(name="user_groups", joinColumns = {
            @JoinColumn(name="userId")},
            inverseJoinColumns = { @JoinColumn(name = "accessGroupId")
            })
    private Collection<AccessGroup> accessGroups;

    public Collection<AccessGroup> getAccessGroups() {
        return accessGroups;
    }

    public void setAccessGroups(Collection<AccessGroup> access_groups) {
        this.accessGroups = access_groups;
    }

....再次,其他方法不相关。

的persistence.xml

 <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    <persistence-unit name="liveDB" transaction-type="RESOURCE_LOCAL">
        <properties>

            <property name="hibernate.connection.url" value="jdbc:sqlserver://#############"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
            <property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
            <property name="hibernate.connection.username" value="#############"/>
            <property name="hibernate.connection.password" value="#############"/>

            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.use_sql_comments" value="true"/>

        </properties>
    </persistence-unit>

</persistence>

这是我用来创建用户(所有者)方面的测试,持久和更新关系的代码。

AccessGroup newGroup = new AccessGroup();
newGroup.setName("The Group");
newGroup.persist();

Collection<AccessGroup> groupList = new ArrayList<AccessGroup>();
groupList.add(newGroup);

User newUser = new User();
newUser.setFirstName("Test");
newUser.setLastName("User");
newUser.setEmail("test@user.com");
newUser.setAccessGroups(groupList);
newUser.persist();

可以完美识别和创建数据库。

如果我去运行查询以检索用户对象,我会得到一个正确的访问组列表(在其他变体中,我添加了多个组),但相反,如果我查询对于AccessGroups,&#34;用户&#34;列表是空的。

表结构按原样创建,使用名为user_groups的连接表,从AccessGroup和AppUsers表中保存ID。我已经在其他框架中运行了这个例子,从互联网上复制并粘贴了其他用于Hibernate m:m映射的例子,我无法让它们中的任何一个正确运行。

我做错了什么?

0 个答案:

没有答案