Java PSQLException: 错误: 语法错误在或附近
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40399460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PSQLException: ERROR: syntax error at or near
提问by why_vincent
I have what I thought was a straight forward relation in JPA. Looks like this. CompanyGroup:
我认为在 JPA 中有一种直接的关系。看起来像这样。公司集团:
@Entity
@Table
public class CompanyGroup implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Column(name = "name")
private String name;
@JoinColumn(name = "companies")
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Company> companies;
}
Company:
公司:
@Entity
@Table
public class Company implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "name")
private String name;
@JoinColumn(name = "users")
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<User> users;
@Id
@GeneratedValue
private Long id;
}
User:
用户:
@Entity
@Table
public class User {
@Column(name = "firstName")
private String firstName;
@Column(name = "lastName")
private String lastName;
@Column(name = "email")
private String email;
@Id
@GeneratedValue
private Long id;
}
I have omitted setters, getters, etc.
我省略了 setter、getter 等。
This is not working. I'm trying to save a CompanyGroup(Has 2 companies, each company has 2 users, all entities are unique) to a fully empty database.
这是行不通的。我正在尝试将 CompanyGroup(有 2 个公司,每个公司有 2 个用户,所有实体都是唯一的)保存到一个完全空的数据库中。
I persist this using Spring-Data, accessed in a service like this:
我使用 Spring-Data 坚持这一点,在这样的服务中访问:
@Service
public class ConcreteCompanyGroupService implements CompanyGroupService {
@Autowired
private CompanyGroupRepository repository;
@Transactional
@Override
public void save(CompanyGroup group) {
repository.save(Collections.singleton(group));
}
}
When I try to call this method I receive this:
当我尝试调用此方法时,我收到以下信息:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "User"
Position: 13
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2458)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2158)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:291)
Hopefully I have done something stupid that someone can find quickly. I don't know how to solve this.
希望我做了一些愚蠢的事情,有人可以很快找到。我不知道如何解决这个问题。
EDIT:
编辑:
The driver in my pom.xml:
我的 pom.xml 中的驱动程序:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1211</version>
</dependency>
采纳答案by Neil Stockton
Your entity maps across to a table name that is an SQL reserved keyword (User
). Sadly for you, your chosen JPA provider does not automatically quote the table name identifier, and so you get exceptions when referring to the table.
您的实体映射到作为 SQL 保留关键字 ( User
)的表名。遗憾的是,您选择的 JPA 提供程序不会自动引用表名标识符,因此在引用表时会出现异常。
Solution is either to quote the table name yourself in the @Table
annotation, or change the table name to not be a reserved keyword. Alternatively use a JPA provider that auto-quotes such reserved keywords for you (e.g DataNucleus
)
解决方案要么在@Table
注释中自己引用表名,要么将表名更改为不是保留关键字。或者使用自动引用此类保留关键字的 JPA 提供程序(例如DataNucleus
)
回答by KeyMaker00
Solution 1: As Pascal mentioned, you have to escape the table name with backslash like:
解决方案 1:正如 Pascal 所提到的,您必须使用反斜杠对表名进行转义,例如:
@Entity
@Table(name="\"User\"")
public class User {
...
}
Solution 2: Rename your table's anme with another name (Users
)
解决方案 2:使用另一个名称 ( Users
)重命名您的表的名称
@Entity
@Table(name="Users")
public class User {
...
}
Solution 3: Add a suffix to the table's name:
解决方案 3:为表名添加后缀:
@Entity
@Table(name="APP_User")
public class User {
...
}
Solution 4: Change the entity name, e.g. ApplicationUser
解决方案 4:更改实体名称,例如ApplicationUser
@Entity
public class ApplicationUser {
...
}
The reason
原因
PostgreSQL as some reserved SQL Key Words. For example: ABORT, ALL, ARRAY, CACHE, CUBE, USER, ... Those tokens are in the SQL standard or specific to PostgreSQL
PostgreSQL 作为一些保留的SQL 关键字。例如:ABORT、ALL、ARRAY、CACHE、CUBE、USER、...这些标记在 SQL 标准中或特定于 PostgreSQL