Java Don';t在web应用程序spring mvc+中获取所有用户对象;jpa&x2B;postgreSQL
我使用SpringMVC、JPA、postgreSQL开发Web应用程序 以下是一些代码: 文件:user.javaJava Don';t在web应用程序spring mvc+中获取所有用户对象;jpa&x2B;postgreSQL,java,spring,hibernate,spring-mvc,jpa,Java,Spring,Hibernate,Spring Mvc,Jpa,我使用SpringMVC、JPA、postgreSQL开发Web应用程序 以下是一些代码: 文件:user.java @Entity @Table(name = "USER") public class User implements Serializable{ private Long id; private String name; private DateTime birthDate; private String address; private
@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;
}
}
@实体
@表(name=“USER”)
公共类用户实现可序列化{
私人长id;
私有字符串名称;
私人日期时间出生日期;
私有字符串地址;
私人字符串电子邮件;
私人电话;
私有字符串用户名;
私有字符串密码;
私有布尔非活动;
私人字符串lastModifiedPerson;
私有日期时间创建日期;
私有日期时间lastModifiedDate;
私有int版本;
私有集角色=新HashSet();
private Set entrys=new HashSet();
private Set comments=new HashSet();
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“ID”)
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
@列(name=“name”)
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
@列(name=“生日”)
@Type(Type=“org.jadira.usertype.dateandtime.joda.PersistentDateTime”)
@DateTimeFormat(iso=iso.DATE)
public DateTime getBirthDate(){
返回出生日期;
}
公共无效setBirthDate(日期时间出生日期){
this.birthDate=生日;
}
@列(name=“ADDRESS”)
公共字符串getAddress(){
回信地址;
}
公共无效设置地址(字符串地址){
this.address=地址;
}
@列(name=“EMAIL”)
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
@列(name=“PHONE”)
公共字符串getPhone(){
回电话;
}
公用无效设置电话(字符串电话){
this.phone=电话;
}
@列(name=“USERNAME”)
公共字符串getUsername(){
返回用户名;
}
public void setUsername(字符串用户名){
this.username=用户名;
}
@列(name=“PASSWORD”)
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
@列(name=“ISACTIVE”)
公共布尔isActive(){
回报是积极的;
}
public void setActive(布尔值isActive){
this.isActive=isActive;
}
@列(name=“上次修改人”)
公共字符串getLastModifiedPerson(){
返回上次修改的人;
}
公共void setLastModifiedPerson(字符串lastModifiedPerson){
this.lastModifiedPerson=lastModifiedPerson;
}
@列(name=“创建日期”)
@Type(Type=“org.jadira.usertype.dateandtime.joda.PersistentDateTime”)
@DateTimeFormat(iso=iso.DATE)
公共日期时间getCreatedDate(){
返回createdDate;
}
public void setCreatedDate(日期时间createdDate){
this.createdDate=createdDate;
}
@列(name=“上次修改日期”)
@Type(Type=“org.jadira.usertype.dateandtime.joda.PersistentDateTime”)
@DateTimeFormat(iso=iso.DATE)
公共日期时间getLastModifiedDate(){
返回最后修改日期;
}
公共void setLastModifiedDate(日期时间lastModifiedDate){
this.lastModifiedDate=lastModifiedDate;
}
@短暂的
公共字符串getBirthDateString(){
字符串birthDateString=“”;
如果(生日!=null)
birthDateString=org.joda.time.format.DateTimeFormat
.forPattern(“yyyy-MM-dd”).print(出生日期);
返回birthDateString;
}
@短暂的
公共字符串getLastModifiedDataTestString(){
字符串birthDateString=“”;
if(lastModifiedDate!=null)
birthDateString=org.joda.time.format.DateTimeFormat
.forPattern(“yyyy-MM-dd”).print(最后修改日期);
返回birthDateString;
}
@短暂的
公共字符串getCreateDateString(){
字符串birthDateString=“”;
如果(createdDate!=null)
birthDateString=org.joda.time.format.DateTimeFormat
.forPattern(“yyyy-MM-dd”).print(创建日期);
返回birthDateString;
}
@许多
@JoinTable(name=“USER\u ROLE”,
joinColumns=@JoinColumn(name=“USER\u ID”),
inverseJoinColumns=@JoinColumn(name=“ROLE\u ID”))
公共集getRoles(){
返回角色;
}
公共无效集合角色(集合角色){
this.roles=角色;
}
@OneToMany(mappedBy=“user”,cascade=CascadeType.ALL,orphan=true)
公共集getEntrys(){
返回入口;
}
公共无效集合入口(集合入口){
this.entrys=entrys;
}
@OneToMany(mappedBy=“user”,cascade=CascadeType.ALL,orphan=true)
公共集getComments(){
返回评论;
}
公共void setComments(Set comments){
this.comments=注释;
}
@版本
@列(name=“VERSION”)
public int getVersion(){
返回版本;
}
公共无效设置版本(int版本){
this.version=版本;
}
@凌驾
公共字符串toString(){
返回“User-Id:+Id+”,Name:+Name+,Username:+Username+,CreatedDate:+CreatedDate;
}
}
文件: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> {
}
package com.software.booksocial.repository;
导入org.springframework.data.repository.crudepository;
导入com.software.booksocial.domain.User;
公共接口UserRepository扩展了Crudepository{
}
文件: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);
}
package com.software.booksocial.service;
导入java.util.List;
导入com.software.booksocial.domain.User;
公共接口用户服务{
列出findAll();
用户findById(长id);
用户保存(User-User);
}
文件: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;
//}
}
package com.software.booksocial.service.jpa;
导入java.util.List;
导入org.springframework.beans.fa
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";
}
}
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="ID")
public Long getId() {
return id;
}