5

我按照本教程在我的域模型中实现了一个带有额外列的多对多关系。它工作得很好,但我无法创建一个条件来查询我的关系左侧的字段。

使用此代码

        @Entity
        @Table( name = "projects")
        public class Project implements Cloneable, Serializable{

            private Long id;
            private String name;
            private Set<ProjectOrganization> projectOrganizations = new HashSet<ProjectOrganization>(0);

            @Id
            @GeneratedValue(strategy = GenerationType.AUTO)
            @Column(nullable = false)
            public Long getId() {
                return this.id;
            }


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


            @Column(name = "name", length = 255, nullable = false)
            public String getName() {
                return this.name;
            }


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


            @OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.project")
            @Cascade(value = { CascadeType.ALL })
            public Set<ProjectOrganization> getProjectOrganizations() {
                return this.projectOrganizations;
            }


            public void setProjectOrganizations(Set<ProjectOrganization> organizationProjects) {
                this.projectOrganizations = organizationProjects;
            }

        }





    @Entity
    @Table(name = "projects_has_organizations")
    @AssociationOverrides({ @AssociationOverride(name = "pk.project", joinColumns = @JoinColumn(name = "projects_id")), 
                            @AssociationOverride(name = "pk.organization", joinColumns = @JoinColumn(name = "organizations_id"))
                            })
    public class ProjectOrganization implements Cloneable, Serializable {


        private ProjectOrganizationPK pk = new ProjectOrganizationPK();
        private OrganizationRolesEnum role;

        public ProjectOrganization() {
        }

        @Transient
        public Organization getOrganization() {
            return getPk().getOrganization();
        }

        public void setOrganization(Organization organization) {
            getPk().setOrganization(organization);
        }

        @EmbeddedId
        public ProjectOrganizationPK getPk() {
            return pk;
        }

        public void setPk(ProjectOrganizationPK pk) {
            this.pk = pk;
        }

        @Transient
        public Project getProject() {
            return getPk().getProject();
        }

        public void setProject(Project project) {
            getPk().setProject(project);
        }

        @Enumerated(EnumType.STRING)
        @Column(nullable = false, length = 50)
        public OrganizationRolesEnum getRole() {
            return role;
        }

        public void setRole(OrganizationRolesEnum role) {
            this.role = role;
        }

    }

    @Embeddable
    public class ProjectOrganizationPK implements Cloneable, Serializable {

        /** Generated serial version UID */
        private static final long serialVersionUID = -4534322563105003365L;

        private Organization organization;
        private Project project;

        @ManyToOne
        public Organization getOrganization() {
            return organization;
        }

        public void setOrganization(Organization organization) {
            this.organization = organization;
        }

        @ManyToOne
        public Project getProject() {
            return project;
        }

        public void setProject(Project project) {
            this.project = project;
        }
    }

    @Entity
    @Table(name = "organizations")
    public class Organization implements Cloneable, Serializable {

        private Long id;
        private String name;
        private Set<ProjectOrganization> projectOrganizations = new HashSet<ProjectOrganization>(0);

        public Organization() {
        }


        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(nullable = false)
        @Override
        public Long getId() {
            return this.id;
        }

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

        @Column(name = "name", nullable = false, length = 255)
        @NotNull(message = "A name is required!")
        public String getName() {
            return this.name;
        }

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


        @OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.organization")
        public Set<ProjectOrganization> getProjectOrganization() {
            return this.projectOrganizations;
        }


        public void setProjectOrganization(Set<ProjectOrganization> projectOrganizations) {
            this.projectOrganizations = projectOrganizations;
        }
}

我想要的是创建一个标准来选择一个Project具有organization请求名称的。

final Criteria crit = getSession().createCriteria(Project.class);
crit.createCriteria("projectOrganizations", "projectOrganization").
 createAlias("pk.organization", "organization").
  add( Restrictions.like("organization.name", "TEST"));

但是当我运行这段代码时,我有这个错误

2012-10-19 10:38:43,095 错误 [org.hibernate.util.JDBCExceptionReporter]“where 子句”中的未知列“organizati2_.name”

而且hibernate生成的sql查询不完整,没有加入projects_has_organizations.organization和organization.id..所以找不到列organization.name

SELECT
   ....
FROM
    projects this_
INNER JOIN projects_has_organizations projectorg1_ ON this_.id = projectorg1_.projects_id
WHERE
    projectorg1_.role =?
AND organizati2_. NAME LIKE ?
ORDER BY
    this_.publish_date DESC

这段代码有什么问题?如何使用条件构建查询?

4

1 回答 1

0

我怀疑问题是由于延迟获取,尝试明确告诉休眠急切地获取您需要的属性。这是通过方法完成的

.setFetchMode("propertyName", FetchMode.EAGER)

因此,换句话说,请尝试急切地获取组织属性:)

于 2012-10-19T16:44:40.283 回答