SpringBoot异常,postgresql,hibernate

时间:2016-12-09 07:16:45

标签: java spring postgresql hibernate rest

你可以帮帮我吗?我无法将用户保存到DB(Postgres)。每次我收到新错误。当我尝试调用方法repo.save()时,程序崩溃了。

WebController.java

@RestController
public class WebController {

@Autowired
UserRepository repository;

private JSONObject jsonObject;
private JSONArray jsonArray;


@RequestMapping(value = "/list", method = RequestMethod.GET)
public String findAll() {
    String result = "<html>";

    for (User user : repository.findAll()) {
        result += "<div>" + user.toString() + "</div>";
    }

    return result + "</html>";
}

@RequestMapping(value = "/get", method = RequestMethod.GET)
public String findById(@RequestParam("id") long id) {
    String result = "";
    result = repository.findOne(id).toString();
    return result;
}

@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
public String deleteById(@RequestParam("id") long id) {
    User user = repository.findOne(id);
    for (Role roles : user.getRoles())
        roles.getUsers().remove(user);
    repository.delete(user);
    return "User was deleted";
}

@RequestMapping(value = "add", method = RequestMethod.POST)
public String create(@RequestBody AddUserRequestBody addUserRequestBody) {
    User user = new User();
    user.setName(addUserRequestBody.getName());
    user.setLogin(addUserRequestBody.getLogin());
    user.setPassword(addUserRequestBody.getPassword());
    repository.save(user);
    return "done";
}

private Map<String, Object> makeMap(String key, Object value) {
    Map<String, Object> map = new HashMap<>();
    map.put(key, value);
    return map;
}


}

User.java

@Entity
@Table(name = "users")
public class User implements Serializable {

private static final long serialVersionUID = -3009157732242241606L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;

@Column(name = "name")
private String name;

@Column(name = "login")
private String login;

@Column(name = "password")
private String password;

@ManyToMany(fetch = FetchType.EAGER, mappedBy = "users")
private List<Role> roles = new ArrayList<Role>();

public User() {
}

public User(String name, String login, String password) {
    this.name = name;
    this.login = login;
    this.password = password;
}



public List<Role> getRoles() {
    return roles;
}

@Override
public String toString() {
    String rolesToString = " ";

    for (Role role : roles) {
        rolesToString += role.getId() + " ";
    }
    return "User{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", login='" + login + '\'' +
            ", password='" + password + '\'' +
            ", roles=" + rolesToString.trim() +
            '}';
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getLogin() {
    return login;
}

public void setLogin(String login) {
    this.login = login;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public void setRoles(List<Role> roles) {
    this.roles = roles;
}
}

Role.java

@Entity
@Table(name = "roles")
public class Role implements Serializable {
private static final long serialVersionUID = 35235235241606L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@Column(name = "name")
private String name;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_roles",
        joinColumns = @JoinColumn(name="role_id", referencedColumnName="id"),
        inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName="id")
)
private List<User> users = new ArrayList<User>();

public Role() {
}

public int getId() {
    return id;
}

public Role(String name, List<User> users) {
    this.name = name;
    this.users = users;
}

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

public void setName(String name) {
    this.name = name;
}

public void setUsers(List<User> users) {
    this.users = users;
}

public List<User> getUsers() {
    return users;
}
}

Application.properties

spring.datasource.url=jdbc:postgresql://somehost/test
spring.datasource.username=test_user
spring.datasource.password=test_user
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-           strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.database=H2
spring.jpa.show-sql=true

hibernate.cfg.xml中

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

             org.hibernate.dialect.PostgreSQLDialect         org.postgresql.Driver         test_user         test_user                  JDBC:在PostgreSQL://为myhost:5432 /测试         1

    <property name="hbm2ddl.auto">create</property>

    <property name="show_sql">true</property>



    <mapping class="com.springjpa.model.User"/>

</session-factory>

UserRepository.class

public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByName(String name);
}

错误

 Позиция: 1
2016-12-09 09:35:00.990 ERROR 3420 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

org.postgresql.util.PSQLException: ERROR: syntax error at or near "call"
  Позиция: 1
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at   org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2155) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
   etc... etc...

0 个答案:

没有答案