我收到错误消息“列“ systementi0_.Id不存在”。我正在使用PostgreSql和Hibernate。 我的实体有代码:
@Entity
@Table(name = "system_table", schema = "blue_schema")
public class SystemEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id", unique = true)
private int id;
@Column(name = "name")
private String name;
@Column(name = "sys_description")
private String systemDescription;
@Column(name = "tech_description")
private String technologyDescritpion;
@Column(name = "owner_name")
private String owner;
public SystemEntity() {
// TODO Auto-generated constructor stub
}
public SystemEntity(int id, String name, String systemDescription, String technologyDescritpion, String owner) {
super();
this.id = id;
this.name = name;
this.systemDescription = systemDescription;
this.technologyDescritpion = technologyDescritpion;
this.owner = owner;
}
public SystemEntity(String name, String systemDescription, String technologyDescritpion, String owner) {
super();
this.name = name;
this.systemDescription = systemDescription;
this.technologyDescritpion = technologyDescritpion;
this.owner = owner;
}
一开始,我正试图从数据库中获取该表中的所有数据。
@Override
public List<SystemEntity> getAllSystems() {
Session currentSession = sessionFactory.getCurrentSession();
Query<SystemEntity> theQuery = currentSession.createQuery("from SystemEntity", SystemEntity.class);
List<SystemEntity> listOfSystems = theQuery.getResultList();
return listOfSystems;
}
这是我的数据库类型和输出: Table output
我的架构名称已在@Table批注中阐明,但仍然出现错误:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/ContractManager] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
org.postgresql.util.PSQLException: Error: columnsystementi0_.id does not exist.
我看到了类似我的帖子,但解决方案很简单,只需在@Table中添加架构名称。
答案 0 :(得分:1)
这是一个常见的错误。第一条建议:不要在所有数据库实体(架构,表或列)的名称中使用大写字母。
您的id
列名称为“ ID ”。尝试“ id ”。
第二个:我建议您在postresql中使用序列生成器:
@Entity
@Table(name = "system_table", schema = "blue_schema")
@SequenceGenerator(schema = "blue_schema", name = "system_table_seq", allocationSize = 1, sequenceName = "system_table_seq")
public class SystemEntity {
// other code below
// ...
}
最后一条建议:不要在JPA实体中使用原始类型。 尝试:
@Entity
@Table(name = "system_table", schema = "blue_schema")
@SequenceGenerator(schema = "blue_schema", name = "system_table_seq", allocationSize = 1, sequenceName = "system_table_seq")
public class SystemEntity {
@Id
@GeneratedValue(generator = "system_table_seq", strategy = GenerationType.SEQUENCE)
private Integer id;
// other code below
// ...
}
答案 1 :(得分:0)
也许您应该使用Integer
数据类型而不是int
,并且应该尝试直接使用表名。 system_table