简体   繁体   中英

Hibernate Persist Object Hieararchy Not Working- AppFuse

It is been a long time I hung up in the following issue. I have a one to many relationship and I am generating the database according to the model using Hibernate. It gives the following error. But it seems I am doing it correct and nothing wrong with it. And I am using the generated app using AppFuse.

Field 'task_id' doesn't have a default value
org.hibernate.exception.GenericJDBCException: could not execute statement

Here are my two models.

User:

@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;
    }

Task:

@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;
    }

This is my service.

    @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);
    }

I have not added the the complete code above, The following is my table creation schema.

       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;

All the answers to this particular question was something to do with AUTO_INCREMENT but I have it all but still it is not working. Any help would be appreciated.

Thanks

This is not the exact answer to the question, But this more general issue. So, I am going to give a general way of making things easier to fix your own bug that goes close with my issue. First off, The basic idea of Spring is to enforce the best practices and make the application more flexible. AppFuse is one way to fast track a web application with user management.

I was doing it right from the beginning, But the problems was that, I was not getting any more information on the debug console, So that I can get a better idea on the case.

I un-commented

<logger name="org.hibernate">
    <level value="WARN"/>
</logger>

<logger name="org.hibernate.SQL">
    <level value="DEBUG"/>
</logger>

You will get a better understanding of where the problem is. :), Happy debugging.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM