i am following the example at
http://struts.apache.org/2.1.8.1/docs/struts-2-spring-2-jpa-ajax.html.
in this example, a very simple persistence is given (of which i am
thankful). but, what i am trying to do is slightly more complicated
with persistence. i need to persist a one-to-many relationship. when i
attempt to do so, i keep getting the following error.

Caused by: java.sql.BatchUpdateException: Cannot add or update a child
row: a foreign key constraint fails (`demo`.`employee`, CONSTRAINT
`employee_ibfk_1` FOREIGN KEY (`companyId`) REFERENCES `company`
(`id`))

what am i doing wrong?

i am using mysql, and these are my DDLs.
create table company (
        name varchar(100) unique,
        id mediumint not null auto_increment,
        primary key(id),
        index using btree(id)
);

create table employee(
        companyId mediumint,
        firstName varchar(100),
        lastName varchar(100),
        id mediumint not null auto_increment,
        primary key(id),
        index using btree(id),
        foreign key (companyId) references company(id)
);

these are my classes for Company and Employee.

@Entity
@Table(name="company")
public class Company {

        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int id;
        private String name;
        @OneToMany(mappedBy="companyId",
                        cascade={CascadeType.REMOVE, CascadeType.PERSIST},
                        targetEntity=Employee.class,
                        fetch=FetchType.EAGER)
        @JoinColumn(name="companyId")
        @ForeignKey(name="id",inverseName="companyId")
        private List<Employee> employees;
        
        public Company() { }
        
        public Company(String name) {
                this.name = name;
        }
        
        public int getId() {
                return id;
        }
        public void setId(int id) {
                this.id = id;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        
        public void addEmployee(Employee employee) {
                List<Employee> employees = getEmployees();
                employees.add(employee);
                
                employee.setCompanyId(getId());
        }

        public List<Employee> getEmployees() {
                if(null == employees) {
                        employees = new ArrayList<Employee>();
                }
                return employees;
        }

        public void setEmployees(List<Employee> employees) {
                this.employees = employees;
        }
        
}

@Entity
@Table(name="employee")
public class Employee {

        private int companyId;
        @Id
        private int id;
        private String firstName;
        private String lastName;
        
        public Employee() { }
        
        public Employee(String firstName, String lastName) {
                this.firstName = firstName;
                this.lastName = lastName;
        }

        public int getCompanyId() {
                return companyId;
        }

        public void setCompanyId(int companyId) {
                this.companyId = companyId;
        }

        public int getId() {
                return id;
        }

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

        public String getFirstName() {
                return firstName;
        }

        public void setFirstName(String firstName) {
                this.firstName = firstName;
        }

        public String getLastName() {
                return lastName;
        }

        public void setLastName(String lastName) {
                this.lastName = lastName;
        }
}

my service implementation that keeps failing to persist is as follows.
any help would be greatly appreciated.

@Transactional
public class CompanyDao {
        
        @PersistenceContext
        protected EntityManager entityManager;
        
        public CompanyDao() { }
        
        public CompanyDao(EntityManager entityManager) {
                this.entityManager = entityManager;
        }

        public void create(Company company) {
                EntityManager em = getEntityManager();
                em.persist(company);
        }
        
        public Company read(int id) {
                EntityManager em = getEntityManager();
                Company company = em.find(Company.class, id);
                return company;
        }
        
        public void update(Company company) {
                EntityManager em = getEntityManager();
                em.merge(company);
        }
        
        public void delete(int id) {
                Company company = read(id);
                if(null != company) {
                        EntityManager em = getEntityManager();
                        em.remove(company);
                }
        }

        public EntityManager getEntityManager() {
                return entityManager;
        }

        public void setEntityManager(EntityManager entityManager) {
                this.entityManager = entityManager;
        }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to