我使用spring3和hibernate4创建了一个项目。但它无法运行。下面提供了代码和根本原因。
@Entity
@Table(name = "Songs")
public class Song implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
Integer id;
@Column(name = "Name", length = 100, nullable = false)
String name;
@Column(name = "Category", length = 100, nullable = true)
String category;
@Column(name = "Singer", length = 100, nullable = true)
String singer;
@Column(name = "Author", length = 100, nullable = true)
String author;
//getter & setter
我有DAO
@Repository("musicDao")
public class MusicDaoImpl implements MusicDao {
private SessionFactory sessionFactory;
private TransactionTemplate transactionTemplate;
public TransactionTemplate getTransactionTemplate() {
return transactionTemplate;
}
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Transactional
public void insert(Song m) {
sessionFactory.getCurrentSession().save(m);
}
@Transactional
public void update(Song m) {
sessionFactory.getCurrentSession().update(m);
}
@Transactional
public void delete(int id) {
// TODO Auto-generated method stub
Song s = (Song) sessionFactory.getCurrentSession().get(Song.class, id);
sessionFactory.getCurrentSession().delete(s);
}
@Transactional
public Song getMusic(int id) {
// TODO Auto-generated method stub
return (Song) sessionFactory.getCurrentSession().get(Song.class, id);
}
@SuppressWarnings("unchecked")
@Transactional
public List<Song> allSong() {
// TODO Auto-generated method stub
// Query query = sessionFactory.getCurrentSession().createQuery("from songs");
// return query.list();
return sessionFactory.getCurrentSession().createQuery("from songs").list();
}
}
我使用MySQL和Hibernate。 我的错误不是地图
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: songs is not mapped [from songs]
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3420)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3309)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
我无法获得allSong方法。为什么Hibernate没有映射???
答案 0 :(得分:1)
"from songs"
实际上是HQL(HIbernate查询语言),因此您应该使用与您的表对应的 OBJECT NAME 。
在这种情况下,请尝试"from Song"