Hibernate OGM(MongoDB)属性解析失败及HQL查询报错求助
Hey there, let's work through this HQL query issue you're hitting with Hibernate OGM and MongoDB. That property resolution error usually boils down to a mismatch between your entity mappings and what you're referencing in the query—let's break down the fixes step by step:
1. Verify Entity Property Names & Getters
First, double-check your Inserogati entity (the one aliased as ie in your query):
- Does it actually have a property named
insegn? Hibernate relies on JavaBean naming conventions, so if your field isinsegn, the getter must begetInsegn()(notgetInsegnamento()or something similar). - Example of a correctly mapped property:
@Entity public class Inserogati { // Other fields... @ManyToOne private Insegn insegn; // Exact property name matches HQL // Correct getter for Hibernate to recognize the property public Insegn getInsegn() { return insegn; } }
If your property is named differently (like insegnamento), you'll need to update your HQL to use that exact name instead of insegn.
2. Confirm Association Mappings
Next, make sure the parent-child associations are properly configured:
- In your
CorsoStudientity, ensure theinserogaticollection is mapped correctly (e.g.,@OneToMany(mappedBy = "corsoStudi")if it's a bidirectional relationship). - Avoid mixing up database field names with entity property names—HQL queries use entity property names, not the actual field names stored in MongoDB (even if you used
@Fieldor@Columnto rename the database field).
3. Simplify the Query to Isolate the Issue
To narrow down the problem, test a stripped-down version of your query first:
SELECT ie.insegn FROM CorsoStudi cs JOIN cs.inserogati ie WHERE cs.nome = 'Laurea in Informatica'
If this simplified query still throws the same "insegn" resolution error, you know the problem is definitely with the Inserogati entity's insegn property mapping. If it works, the issue might be with how you're using COUNT() and GROUP BY (though Hibernate OGM does support these for MongoDB in most cases).
4. Check for Typos or Case Sensitivity
It's easy to miss a typo! Double-check that:
insegnis spelled exactly the same in both your entity and HQL (case matters—Hibernate is case-sensitive with property names).- You didn't accidentally reference a database column name instead of the entity property name.
Example of a Working Setup
If your entities look like this, your original query should work:
// CorsoStudi.java @Entity public class CorsoStudi { @Id private String id; private String nome; @OneToMany(mappedBy = "corsoStudi") private List<Inserogati> inserogati; // Getters and setters } // Inserogati.java @Entity public class Inserogati { @Id private String id; @ManyToOne private CorsoStudi corsoStudi; @ManyToOne private Insegn insegn; // Getters and setters } // Insegn.java @Entity public class Insegn { @Id private String id; private String nome; // Other fields and accessors }
内容的提问来源于stack exchange,提问作者Emanuele Crema

