Hibernate 持久化对象层次结构不工作 - AppFuse
Hibernate Persist Object Hieararchy Not Working- AppFuse
下期挂了好久了。我有一个一对多的关系,我正在根据使用 Hibernate 的模型生成数据库。它给出了以下错误。但似乎我做的是正确的,没有错。我正在使用 AppFuse 生成的应用程序。
Field 'task_id' doesn't have a default value
org.hibernate.exception.GenericJDBCException: could not execute statement
这是我的两个模型。
用户:
@Entity
@Table(name = "app_user")
@Indexed
@XmlRootElement
public class User extends BaseObject implements Serializable, UserDetails {
private static final long serialVersionUID = 3832626162173359411L;
private Long id;
private String username; // required
private String password; // required
private String confirmPassword;
private String passwordHint;
private String firstName; // required
private String lastName; // required
private String email; // required; unique
private String phoneNumber;
private String website;
private Address address = new Address();
private Integer version;
private Set<Role> roles = new HashSet<Role>();
private Set<Task> tasks = new HashSet<Task>();
private boolean enabled;
private boolean accountExpired;
private boolean accountLocked;
private boolean credentialsExpired;
/**
* Default constructor - creates a new instance with no values set.
*/
public User() {
}
/**
* Create a new instance and set the username.
*
* @param username
* login name for user.
*/
public User(final String username) {
this.username = username;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@DocumentId
public Long getId() {
return id;
}
@Column(nullable = false, length = 50, unique = true)
@Field
public String getUsername() {
return username;
}
@Column(nullable = false)
@XmlTransient
@JsonIgnore
public String getPassword() {
return password;
}
@Transient
@XmlTransient
@JsonIgnore
public String getConfirmPassword() {
return confirmPassword;
}
@Column(name = "password_hint")
@XmlTransient
public String getPasswordHint() {
return passwordHint;
}
@Column(name = "first_name", nullable = false, length = 50)
@Field
public String getFirstName() {
return firstName;
}
@Column(name = "last_name", nullable = false, length = 50)
@Field
public String getLastName() {
return lastName;
}
@Column(nullable = false, unique = true)
@Field
public String getEmail() {
return email;
}
@Column(name = "phone_number")
@Field(analyze = Analyze.NO)
public String getPhoneNumber() {
return phoneNumber;
}
@Field
public String getWebsite() {
return website;
}
/**
* Returns the full name.
*
* @return firstName + ' ' + lastName
*/
@Transient
public String getFullName() {
return firstName + ' ' + lastName;
}
任务:
@Entity
@Table(name = "task")
@XmlRootElement
public class Task extends BaseObject {
private static final long serialVersionUID = 2240962937778432578L;
private Long id;
private Long userId;
private User user;
private CSVFile csvFile;
private String status;
private Date addedDate;
private Date completedDate;
private Set<Part> parts = new HashSet<Part>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "task_id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "user_id")
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false, insertable = false, updatable = false)
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@OneToOne(fetch = FetchType.LAZY, mappedBy = "task", cascade = CascadeType.ALL)
public CSVFile getCsvFile() {
return csvFile;
}
public void setCsvFile(CSVFile csvFile) {
this.csvFile = csvFile;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getAddedDate() {
return addedDate;
}
public void setAddedDate(Date addedDate) {
this.addedDate = addedDate;
}
public Date getCompletedDate() {
return completedDate;
}
public void setCompletedDate(Date completedDate) {
this.completedDate = completedDate;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "task", cascade = CascadeType.ALL)
public Set<Part> getParts() {
return parts;
}
public void setParts(Set<Part> parts) {
this.parts = parts;
}
这是我的服务。
@Service("taskService")
public class TaskServiceImpli extends GenericManagerImpl<Task, Long> implements
TaskService {
private UserDao userDao;
private CSVExtractor csvExtractor;
private TaskDao taskDao;
@Autowired
public TaskServiceImpli(TaskDao taskDao) {
super(taskDao);
this.taskDao = taskDao;
}
public CSVExtractor getCsvExtractor() {
return csvExtractor;
}
@Autowired
public void setCsvExtractor(CSVExtractor csvExtractor) {
this.csvExtractor = csvExtractor;
}
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public List<Task> getAllTasksPerUser(String userId) {
// TODO Auto-generated method stub
return null;
}
@Override
public Task saveTask(Task task) {
return taskDao.saveTask(task);
}
我没有添加上面的完整代码,下面是我的table创建模式。
CREATE TABLE `task` (
`task_id` bigint(20) NOT NULL AUTO_INCREMENT,
`addedDate` datetime DEFAULT NULL,
`completedDate` datetime DEFAULT NULL,
`status` varchar(255) DEFAULT NULL,
`user_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`task_id`),
KEY `FK3635851EFD5399` (`user_id`),
CONSTRAINT `FK3635851EFD5399` FOREIGN KEY (`user_id`) REFERENCES `app_user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
SELECT * FROM bisco_tool.task;
CREATE TABLE `app_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`account_expired` bit(1) NOT NULL,
`account_locked` bit(1) NOT NULL,
`address` varchar(150) DEFAULT NULL,
`city` varchar(50) DEFAULT NULL,
`country` varchar(100) DEFAULT NULL,
`postal_code` varchar(15) DEFAULT NULL,
`province` varchar(100) DEFAULT NULL,
`credentials_expired` bit(1) NOT NULL,
`email` varchar(255) NOT NULL,
`account_enabled` bit(1) DEFAULT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`password_hint` varchar(255) DEFAULT NULL,
`phone_number` varchar(255) DEFAULT NULL,
`username` varchar(50) NOT NULL,
`version` int(11) DEFAULT NULL,
`website` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
这个特定问题的所有答案都与 AUTO_INCREMENT 有关,但我已经知道了所有答案,但仍然无法正常工作。任何帮助,将不胜感激。
谢谢
这不是问题的确切答案,而是一个更普遍的问题。因此,我将提供一种通用方法,使事情更容易修复您自己的与我的问题接近的错误。
首先,Spring 的基本思想是实施最佳实践并使应用程序更加灵活。 AppFuse 是一种通过用户管理快速跟踪 Web 应用程序的方法。
我从一开始就做对了,但问题是,我没有在调试控制台上获得更多信息,所以我可以更好地了解这个案例。
我取消评论
<logger name="org.hibernate">
<level value="WARN"/>
</logger>
<logger name="org.hibernate.SQL">
<level value="DEBUG"/>
</logger>
您将更好地了解问题出在哪里。 :), 调试愉快。
下期挂了好久了。我有一个一对多的关系,我正在根据使用 Hibernate 的模型生成数据库。它给出了以下错误。但似乎我做的是正确的,没有错。我正在使用 AppFuse 生成的应用程序。
Field 'task_id' doesn't have a default value
org.hibernate.exception.GenericJDBCException: could not execute statement
这是我的两个模型。
用户:
@Entity
@Table(name = "app_user")
@Indexed
@XmlRootElement
public class User extends BaseObject implements Serializable, UserDetails {
private static final long serialVersionUID = 3832626162173359411L;
private Long id;
private String username; // required
private String password; // required
private String confirmPassword;
private String passwordHint;
private String firstName; // required
private String lastName; // required
private String email; // required; unique
private String phoneNumber;
private String website;
private Address address = new Address();
private Integer version;
private Set<Role> roles = new HashSet<Role>();
private Set<Task> tasks = new HashSet<Task>();
private boolean enabled;
private boolean accountExpired;
private boolean accountLocked;
private boolean credentialsExpired;
/**
* Default constructor - creates a new instance with no values set.
*/
public User() {
}
/**
* Create a new instance and set the username.
*
* @param username
* login name for user.
*/
public User(final String username) {
this.username = username;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@DocumentId
public Long getId() {
return id;
}
@Column(nullable = false, length = 50, unique = true)
@Field
public String getUsername() {
return username;
}
@Column(nullable = false)
@XmlTransient
@JsonIgnore
public String getPassword() {
return password;
}
@Transient
@XmlTransient
@JsonIgnore
public String getConfirmPassword() {
return confirmPassword;
}
@Column(name = "password_hint")
@XmlTransient
public String getPasswordHint() {
return passwordHint;
}
@Column(name = "first_name", nullable = false, length = 50)
@Field
public String getFirstName() {
return firstName;
}
@Column(name = "last_name", nullable = false, length = 50)
@Field
public String getLastName() {
return lastName;
}
@Column(nullable = false, unique = true)
@Field
public String getEmail() {
return email;
}
@Column(name = "phone_number")
@Field(analyze = Analyze.NO)
public String getPhoneNumber() {
return phoneNumber;
}
@Field
public String getWebsite() {
return website;
}
/**
* Returns the full name.
*
* @return firstName + ' ' + lastName
*/
@Transient
public String getFullName() {
return firstName + ' ' + lastName;
}
任务:
@Entity
@Table(name = "task")
@XmlRootElement
public class Task extends BaseObject {
private static final long serialVersionUID = 2240962937778432578L;
private Long id;
private Long userId;
private User user;
private CSVFile csvFile;
private String status;
private Date addedDate;
private Date completedDate;
private Set<Part> parts = new HashSet<Part>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "task_id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "user_id")
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false, insertable = false, updatable = false)
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@OneToOne(fetch = FetchType.LAZY, mappedBy = "task", cascade = CascadeType.ALL)
public CSVFile getCsvFile() {
return csvFile;
}
public void setCsvFile(CSVFile csvFile) {
this.csvFile = csvFile;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getAddedDate() {
return addedDate;
}
public void setAddedDate(Date addedDate) {
this.addedDate = addedDate;
}
public Date getCompletedDate() {
return completedDate;
}
public void setCompletedDate(Date completedDate) {
this.completedDate = completedDate;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "task", cascade = CascadeType.ALL)
public Set<Part> getParts() {
return parts;
}
public void setParts(Set<Part> parts) {
this.parts = parts;
}
这是我的服务。
@Service("taskService")
public class TaskServiceImpli extends GenericManagerImpl<Task, Long> implements
TaskService {
private UserDao userDao;
private CSVExtractor csvExtractor;
private TaskDao taskDao;
@Autowired
public TaskServiceImpli(TaskDao taskDao) {
super(taskDao);
this.taskDao = taskDao;
}
public CSVExtractor getCsvExtractor() {
return csvExtractor;
}
@Autowired
public void setCsvExtractor(CSVExtractor csvExtractor) {
this.csvExtractor = csvExtractor;
}
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public List<Task> getAllTasksPerUser(String userId) {
// TODO Auto-generated method stub
return null;
}
@Override
public Task saveTask(Task task) {
return taskDao.saveTask(task);
}
我没有添加上面的完整代码,下面是我的table创建模式。
CREATE TABLE `task` (
`task_id` bigint(20) NOT NULL AUTO_INCREMENT,
`addedDate` datetime DEFAULT NULL,
`completedDate` datetime DEFAULT NULL,
`status` varchar(255) DEFAULT NULL,
`user_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`task_id`),
KEY `FK3635851EFD5399` (`user_id`),
CONSTRAINT `FK3635851EFD5399` FOREIGN KEY (`user_id`) REFERENCES `app_user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
SELECT * FROM bisco_tool.task;
CREATE TABLE `app_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`account_expired` bit(1) NOT NULL,
`account_locked` bit(1) NOT NULL,
`address` varchar(150) DEFAULT NULL,
`city` varchar(50) DEFAULT NULL,
`country` varchar(100) DEFAULT NULL,
`postal_code` varchar(15) DEFAULT NULL,
`province` varchar(100) DEFAULT NULL,
`credentials_expired` bit(1) NOT NULL,
`email` varchar(255) NOT NULL,
`account_enabled` bit(1) DEFAULT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`password_hint` varchar(255) DEFAULT NULL,
`phone_number` varchar(255) DEFAULT NULL,
`username` varchar(50) NOT NULL,
`version` int(11) DEFAULT NULL,
`website` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
这个特定问题的所有答案都与 AUTO_INCREMENT 有关,但我已经知道了所有答案,但仍然无法正常工作。任何帮助,将不胜感激。
谢谢
这不是问题的确切答案,而是一个更普遍的问题。因此,我将提供一种通用方法,使事情更容易修复您自己的与我的问题接近的错误。 首先,Spring 的基本思想是实施最佳实践并使应用程序更加灵活。 AppFuse 是一种通过用户管理快速跟踪 Web 应用程序的方法。
我从一开始就做对了,但问题是,我没有在调试控制台上获得更多信息,所以我可以更好地了解这个案例。
我取消评论
<logger name="org.hibernate">
<level value="WARN"/>
</logger>
<logger name="org.hibernate.SQL">
<level value="DEBUG"/>
</logger>
您将更好地了解问题出在哪里。 :), 调试愉快。