我认为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; }
公司:
@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; }
用户:
@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; }
我省略了setter,getters等.
这不起作用.我正在尝试将一个CompanyGroup(有2个公司,每个公司有2个用户,所有实体都是唯一的)保存到一个完全空的数据库.
我坚持使用Spring-Data,在这样的服务中访问:
@Service public class ConcreteCompanyGroupService implements CompanyGroupService { @Autowired private CompanyGroupRepository repository; @Transactional @Override public void save(CompanyGroup group) { repository.save(Collections.singleton(group)); } }
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)
希望我做了一些愚蠢的事情,有人可以很快找到.我不知道如何解决这个问题.
编辑:
我的pom.xml中的驱动程序:
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.4.1211</version> </dependency>