0

我在我的项目中使用Spring-mvc & Spring-data-jpa。我有这两个实体Spring MVC:使用ManyToOne/OneToMany关系存储数据

Location.java:

@Entity 
@Table(name = "location") 
@JsonIgnoreProperties(ignoreUnknown = true) 
public class Location { 
    @Id 
    @GeneratedValue 
    private int id; 

    // other attributes... 

    @ManyToOne(optional=true) 
    @JoinColumn(name ="user") 
    @JsonBackReference 
    private User user; 

    @ManyToOne(optional=true) 
    @JoinColumn(name ="client") 
    @JsonBackReference 
    private Client client; 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    // getters & setters 

} 

User.java:

@Entity 
@Table(name = "users") 
@JsonIgnoreProperties(ignoreUnknown = true) 
public class User { 

    @Id 
    @GeneratedValue 
    private int uid; 

    // other attributes... 

    @OneToMany(mappedBy="user") 
    @JsonManagedReference 
    private List<Location> locations; 

    // getters & setters... 

    @JsonIgnore 
    public List<Location> getLocations() { 
     return locations; 
    } 

    public void setLocations(List<Location> locations) { 
     this.locations = locations; 
    } 

} 

什么我想要做的是增加一个新的位置,并将其链接到现有用户。对于这两个实体都有一个存储库和一个服务。 LocationRepository.java:

public interface LocationRepository extends CrudRepository<Location, Integer> { 

    List<Location> findAll(); 

    //.... 

} 

LocationService.java

@Service 
public class LocationService { 

    @Autowired 
    private LocationRepository locationRepository; 


    public List<Location> findAll() { 
     return locationRepository.findAll(); 
    } 

    public void save(Location location) { 
     locationRepository.save(location); 
    } 

    @Secured("ROLE_ADMIN") 
    public void delete(int locationId) { 
     locationRepository.delete(locationId); 
    } 
} 

ApiController.java

@Controller 
@RequestMapping(value = "/rest/api") 
public class ApiController { 

    @Autowired 
    UserService userService; 
    @Autowired 
    LocationService locationService; 

    // Storing a new location 

    @RequestMapping(value = "/locations/checkin", method = RequestMethod.POST, produces = "application/json") 
    public ResponseEntity<?> checkin(@ModelAttribute("location") Location location) { 
     locationService.save(location); 
     List<Location> locationslist = locationService.findAll(); 
     return new ResponseEntity<List<Location>>(locationslist, HttpStatus.OK); 
    } 

} 

一切似乎罚款至今。我为Android客户端使用Restfull服务,我所要做的就是重新构建一个新的Location对象,并通过POST请求将其发送给正确的URI。

这是对象的结构:

{ 
    "id":1, 
    ...., 
    ...., 
    "user":{ 
      "uid":1, 
      "attr1":"value1", 
      "attr2":"value2", 
      ..... 
      } 
} 

问题:org.postgresql.util.PSQLException:我总是得到一个(org.hibernate.exception.SQLGrammarException)误差(产生的原因:错误:在“用户”或其附近的语法错误)

我做错了什么?什么是存储我的位置的最佳方法?

+0

你可以发布存储库和休眠配置以及 – ikumen

+0

回购配置配置? –

回答

0

的例外规定,无效的SQL语句生成

org.postgresql.util.PSQLException: ERROR: syntax error at or near "user" 

尝试将列“用户”重命名为“用户”是Postgres的保留字,所以不是

@JoinColumn(name ="user") 

使用像

@JoinColumn(name ="user_location")