问题描述
我使用spring MVC,JPA,postgreSQL开发Web应用程序。以下是一些代码:
文件:user.java
c是Postgres正如你在评论中告诉的那样),所以你最好的选择是把表重命名为别的东西(比如 code>语句,但现在可以修改)。
无论如何,请查看获取更多信息。
I uses spring MVC, JPA, postgreSQL develop Web Application
Here is some codes:
File: user.java
@Entity @Table(name = "USER") public class User implements Serializable{ private Long id; private String name; private DateTime birthDate; private String address; private String email; private String phone; private String username; private String password; private boolean isActive; private String lastModifiedPerson; private DateTime createdDate; private DateTime lastModifiedDate; private int version; private Set<Role> roles = new HashSet<Role>(); private Set<Entry> entrys = new HashSet<Entry>(); private Set<EntryComment> comments = new HashSet<EntryComment>(); @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="ID") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="NAME") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="BIRTHDATE") @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime") @DateTimeFormat(iso=ISO.DATE) public DateTime getBirthDate() { return birthDate; } public void setBirthDate(DateTime birthDate) { this.birthDate = birthDate; } @Column(name="ADDRESS") public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Column(name="EMAIL") public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Column(name="PHONE") public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Column(name="USERNAME") public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Column(name="PASSWORD") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Column(name="ISACTIVE") public boolean isActive() { return isActive; } public void setActive(boolean isActive) { this.isActive = isActive; } @Column(name="LAST_MODIFIED_BY") public String getLastModifiedPerson() { return lastModifiedPerson; } public void setLastModifiedPerson(String lastModifiedPerson) { this.lastModifiedPerson = lastModifiedPerson; } @Column(name="CREATE_DATE") @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime") @DateTimeFormat(iso=ISO.DATE) public DateTime getCreatedDate() { return createdDate; } public void setCreatedDate(DateTime createdDate) { this.createdDate = createdDate; } @Column(name="LAST_MODIFIED_DATE") @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime") @DateTimeFormat(iso=ISO.DATE) public DateTime getLastModifiedDate() { return lastModifiedDate; } public void setLastModifiedDate(DateTime lastModifiedDate) { this.lastModifiedDate = lastModifiedDate; } @Transient public String getBirthDateString(){ String birthDateString=""; if(birthDate != null) birthDateString = org.joda.time.format.DateTimeFormat .forPattern("yyyy-MM-dd").print(birthDate); return birthDateString; } @Transient public String getLastModifiedDateString(){ String birthDateString=""; if(lastModifiedDate != null) birthDateString = org.joda.time.format.DateTimeFormat .forPattern("yyyy-MM-dd").print(lastModifiedDate); return birthDateString; } @Transient public String getCreateDateString(){ String birthDateString=""; if(createdDate != null) birthDateString = org.joda.time.format.DateTimeFormat .forPattern("yyyy-MM-dd").print(createdDate); return birthDateString; } @ManyToMany @JoinTable(name = "USER_ROLE", joinColumns = @JoinColumn(name="USER_ID"), inverseJoinColumns=@JoinColumn(name="ROLE_ID")) public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } @OneToMany(mappedBy="user", cascade= CascadeType.ALL, orphanRemoval=true) public Set<Entry> getEntrys() { return entrys; } public void setEntrys(Set<Entry> entrys) { this.entrys = entrys; } @OneToMany(mappedBy="user", cascade = CascadeType.ALL, orphanRemoval = true) public Set<EntryComment> getComments() { return comments; } public void setComments(Set<EntryComment> comments) { this.comments = comments; } @Version @Column(name="VERSION") public int getVersion() { return version; } public void setVersion(int version) { this.version = version; } @Override public String toString(){ return "User - Id:" + id +", Name: "+ name +", Username: " + username +", CreatedDate: " + createdDate; } }File: UserRepository.java
package com.software.booksocial.repository; import org.springframework.data.repository.CrudRepository; import com.software.booksocial.domain.User; public interface UserRepository extends CrudRepository<User, Long> { }File:UserService.java
package com.software.booksocial.service; import java.util.List; import com.software.booksocial.domain.User; public interface UserService { List<User> findAll(); User findById(Long id); User save(User user); }File: UserServiceIml.java
package com.software.booksocial.service.jpa; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.software.booksocial.domain.User; import com.software.booksocial.repository.UserRepository; import com.software.booksocial.service.UserService; import com.google.common.collect.Lists; @Repository @Transactional @Service("userService") public class UserServiceIml implements UserService { @Autowired private UserRepository userRepository; @Override @Transactional(readOnly = true) public List<User> findAll(){ return Lists.newArrayList(userRepository.findAll()); } @Override @Transactional(readOnly=true) public User findById(Long id){ return userRepository.findOne(id); } @Override public User save(User user){ return userRepository.save(user); } //@Autowired //public void setUserRepository(UserRepository userRepository){ // this.userRepository = userRepository; //} }File:UserController.java
package com.software.booksocial.controller; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.software.booksocial.domain.User; import com.software.booksocial.service.UserService; @RequestMapping("/users") @Controller public class UserController { final Logger logger = LoggerFactory.getLogger(UserController.class); @Autowired private UserService userService; @RequestMapping(method = RequestMethod.GET) public String list(Model uiModel){ logger.info("Listing Users"); List<User> users = userService.findAll(); uiModel.addAttribute("users", users); logger.info("No. of users: " + users.size()); return "users/list"; } }And Database Struction in image link:
https://dl.dropboxusercontent.com/u/39630113/DBBookSocial.png
https://dl.dropboxusercontent.com/u/39630113/Picture/DBUpdate.png
Main Error:
And Error Text:
I don't get all user from table USER from UserController using spring data jpa. Thanks for helping.
解决方案user is a reserved keyword in Postgres (as you were told in the comments), so your best option is to rename the table to something else (like APP_USER) and change the entity mapping to @Table(name = "APP_USER").
If you can't/don't wont to change table name, you would have to enclose the table name in double quotes wherever you use it, starting with @Table(name = " \"USER\""), but if I remember correctly this caused some bugs the last time I tried it, when you query other entities that are related to User (double quotes weren't applied in join statements, but that could be fixed by now).
Anyway, check out this thread for more info.
这篇关于不要在Web应用程序spring mvc + jpa + postgreSQL中获取所有用户对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!