I have what I thought was a straight forward relation in JPA. Looks like this. CompanyGroup:
我认为在JPA中是一个直接的关系。看起来是这样的。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 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.
这不是工作。我试图拯救一个公司集团(有2个公司,每个公司有2个用户,所有实体都是唯一的)到一个完全空的数据库。
I persist this using Spring-Data, accessed in a service like this:
我坚持使用spring数据,在这样的服务中访问:
@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:
在我的页面中的驱动程序。xml:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1211</version>
</dependency>
1 个解决方案
#1
10
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提供程序,自动为您引用这些保留的关键字(e)。g DataNucleus)
#1
10
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提供程序,自动为您引用这些保留的关键字(e)。g DataNucleus)