Pertanyaan Hibernate banyak-ke-banyak dengan masalah kriteria extracolumn


Saya mengikuti ini tutorial untuk menerapkan dalam model domain saya hubungan banyak-ke-banyak dengan kolom tambahan. Ini berfungsi dengan baik, tetapi saya tidak dapat membuat kriteria untuk meminta bidang di sisi kiri relasi saya.

Mengambil kode ini

        @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;
        }
}

Saya ingin adalah membuat kriteria untuk memilih Project yang memiliki organization dengan nama yang diminta.

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

Tetapi ketika saya menjalankan kode ini saya memiliki kesalahan ini

2012-10-19 10: 38: 43,095 ERROR [org.hibernate.util.JDBCExceptionReporter] Kolom tidak dikenal 'organizati2_.name' di 'where clause'

dan kueri sql yang dihasilkan oleh hibernate tidak lengkap, tidak bergabung dengan projects_has_organizations.organisasidengan organization.id .. Sehingga tidak dapat menemukan column 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

Apa yang salah dengan kode ini? Bagaimana saya bisa membangun query menggunakan kriteria?


5
2017-10-19 09:01


asal


Jawaban:


Saya menduga bahwa masalahnya adalah karena malas mengambil, coba secara eksplisit memberitahu hibernate untuk dengan bersemangat mengambil properti yang Anda butuhkan. Ini dilakukan dengan metode

.setFetchMode("propertyName", FetchMode.EAGER)

Jadi, di kata lain, cobalah dengan penuh semangat mengambil properti organisasi :)


0
2017-10-19 16:44