I am using Spring-mvc & Spring-data-jpa in my project. I have these two entities
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;
}
}
What i want to do is to add a new Location and link it to an existing User. For both entitie there is a Repository and a Service. 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);
}
}
All seems fine till now. I am using this Restfull service for an Android client, all i have to do is to custruct a new Location object and send it via a POST request to the right URI.
This is the structure of the object :
{
"id":1,
....,
....,
"user":{
"uid":1,
"attr1":"value1",
"attr2":"value2",
.....
}
}
Problem : I am always getting a (org.hibernate.exception.SQLGrammarException) error (Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "user")
What i am doing wrong ? and what is the best approach to store my location ?
The exception states that an invalid SQL statement is generated
org.postgresql.util.PSQLException: ERROR: syntax error at or near "user"
Try to rename the column 'user' to something 'user' is a reserved word in Postgres, so instead of
@JoinColumn(name ="user")
use something like
@JoinColumn(name ="user_location")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.