Java – psqlexception: error: syntax error at or near
I think JPA is a direct relationship It looks like this CompanyGroup:
@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 omitted setters, getters, etc
It doesn't work I'm trying to save a company group (there are 2 companies, each company has 2 users, and all entities are unique) to a completely empty database
I insist on using spring data to access the following services:
@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:
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)
I hope I've done something stupid and someone can find it soon I don't know how to solve the problem
Edit:
My POM Drivers in XML:
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.4.1211</version> </dependency>
Solution
Your entity is mapped to the table name, which is the SQL reserved keyword (user) Unfortunately, the JPA provider you selected does not automatically reference the table name identifier, so an exception occurs when referencing the table
The solution is to reference the table name in the @ table annotation, or change the table name to not a reserved keyword Or use the JPA provider to automatically reference such reserved keywords for you (such as datanucleus)