Java MySQLSyntaxErrorException尽管一切看起来都很好(Hibernate)
我有一个项目实体和一个集团实体。我可以使用以下getter方法从DAO按名称获取项目:Java MySQLSyntaxErrorException尽管一切看起来都很好(Hibernate),java,hibernate,orm,Java,Hibernate,Orm,我有一个项目实体和一个集团实体。我可以使用以下getter方法从DAO按名称获取项目: public Project getProject(String name){ Project project = null; DetachedCriteria criteria = DetachedCriteria.forClass(Project.class); criteria.add(Restrictions.eq("name", name)); List<Pr
public Project getProject(String name){
Project project = null;
DetachedCriteria criteria = DetachedCriteria.forClass(Project.class);
criteria.add(Restrictions.eq("name", name));
List<Project> projects = getHibernateTemplate().findByCriteria(criteria);
if ((projects != null) && (projects.size() > 0)) {
project = (Project)projects.get(0);
}
return project;
}
不起作用的是:
@Entity
@Table(name = "project")
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
@Transient
public static final String REF = "Project";
@ManyToMany(targetEntity = pl.edu.agh.adam.core.projects.hibernate.Tag.class)
@JoinTable(name = "project_tag", joinColumns = @JoinColumn(name = "project_id"), inverseJoinColumns = @JoinColumn(name = "tag_id"))
private List<Tag> tags;
@ManyToMany(targetEntity = pl.edu.agh.adam.core.projects.hibernate.Group.class)
@JoinTable(name = "project_group_role",
joinColumns = @JoinColumn(name = "project_id"),
inverseJoinColumns = @JoinColumn(name = "group_id"))
private Set<Group> groups;
@ManyToMany(targetEntity = pl.edu.agh.adam.core.projects.hibernate.Role.class)
@JoinTable(name = "project_group_role",
joinColumns = @JoinColumn(name = "project_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;
@Transient
public static final String PROP_ID = "id";
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "project_id")
private Long id;
@Transient
public static final String PROP_NAME = "name";
@Column(name = "name", length = 60, unique = true, nullable = false)
private String name;
@Transient
public static final String PROP_SHORTNAME = "shortname";
@Column(name = "shortname", length = 25, unique = true)
private String shortname;
@Transient
public static final String PROP_HOMEPAGE = "homepage";
@Column(name = "homepage", length = 60)
private String homepage;
@Transient
public static final String PROP_DESCRIPTION = "description";
@Column(name = "description", columnDefinition = "LONGTEXT")
private String description;
}
select
this_.group_id as group1_4_0_,
this_.name as name4_0_,
this_.password as password4_0_,
this_.subscribable as subscrib4_4_0_
from
group this_ where
this_.name=?
发生了什么事??:(我没有立即发现这一点,但
group
是一个保留关键字,因此生成的查询确实不正确。请为group
实体使用另一个表名。例如:
@Entity
@Table(name = "groups")
public class Group implements Serializable {
//...
}
第二个查询在粘贴到SQL客户端时是否运行?不,它不会运行,这并不令人惊讶,尽管我不能一眼看出它有什么问题。此外,我如何使hibernate生成一个不同的查询?我不希望它工作,但在SQL客户端中“调试”某些内容(删除别名、删除列等)更容易.无论如何,我想我找到了问题所在。如果这不对的话,该死的。除了GROUP BY之外,GROUP还有其他用法吗?如果没有,这个词应该是可用的。当涉及到修改数据库时,我很不情愿,所以我这样做了:
@Table(name=“`GROUP`”)
现在每件事都很有魅力。非常感谢你帮我发现了这一点:)@sammy:的确,这会起作用(如果你不想更改数据库,实际上效果会更好)。我个人尽量避免保留关键字,但你的解决方案很好。啊,你也帮了我。我的保留字是“索引”。-)
select
this_.project_id as project1_3_0_,
this_.description as descript2_3_0_,
this_.homepage as homepage3_0_,
this_.name as name3_0_,
this_.shortname as shortname3_0_
from
project this_
where
this_.name=?
select
this_.group_id as group1_4_0_,
this_.name as name4_0_,
this_.password as password4_0_,
this_.subscribable as subscrib4_4_0_
from
group this_ where
this_.name=?
@Entity
@Table(name = "groups")
public class Group implements Serializable {
//...
}