Results 1 to 4 of 4

Thread: Cannot add or update a child row: a foreign key constraint fails

  1. #1
    Join Date
    Feb 2010
    Location
    India
    Posts
    14

    Default Cannot add or update a child row: a foreign key constraint fails

    Hi,
    I'm new to Spring and JPA. I am trying to insert a data into two tables with simple many-to-one relation. I'm unable to do this since it fails with the foreign constraint problem.

    My junit test uses following as data initializing class.

    Code:
    @Component
    @Scope("prototype")
    public class DataInitializer {
    
    	@PersistenceContext
    	private EntityManager entityManager;
    
    	public void initData() {
    		people.clear();// clear out the previous list of people
    		addPerson("Jim", "Smith");
    		addPerson("Tina", "Marsh");
    		addPerson("Steve", "Blair");
    	}
    	
    	@Transactional
    	public void addPerson(String firstName, String lastName) {
    		
    		Team t = new Team();
    		t.setName("EXTRA");
    		entityManager.persist(t);
    		System.out.println("***"+t.getId());
    		
    		Person p = new Person();
    		p.setFirstName(firstName);
    		p.setLastName(lastName);
    		p.setPassword("password");
    		p.setEmail("email");
    		p.setCompanyName("Grassfield");
    		p.setUserName(firstName);
    		p.setIsPowerUser(true);
    		
    		
    		p.setTeam(t);
    		
    		entityManager.persist(p);
    		people.add(p.getId());
    	}
    	
    	public EntityManager getEntityManager() {
    		return entityManager;
    	}
    }

    ..to be continued

  2. #2
    Join Date
    Feb 2010
    Location
    India
    Posts
    14

    Default

    Person class

    Code:
    @Entity
    @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
    public class Person implements Serializable {
    
    	private static final long serialVersionUID = -1308795024262635690L;
    
    	@Id
    	@GeneratedValue(strategy = GenerationType.AUTO)
    	private Long id;
    
    	@Column
    	private String firstName;
    
    	@Column
    	private String lastName;
    	
    	@Column(unique=true)
    	@NotEmpty
    	@NotNull
    	private String userName;
    	
    	@Column
    	private String password;
    	
    	@Column
    	private String email;
    	
    	@Column
    	private String companyName;
    	
    	@Column
    	private Boolean isPowerUser;
    	
    	@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    	@PrimaryKeyJoinColumn
    	private Team team;
    	
    	public Person() {
    	}
    
    	public Person(String firstName, String lastName) {
    		super();
    		this.firstName = firstName;
    		this.lastName = lastName;
    	}
    
    	public Long getId() {
    		return id;
    	}
    
    	public void setId(Long 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;
    	}
    
    	public String getUserName() {
    		return userName;
    	}
    
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    	public String getEmail() {
    		return email;
    	}
    
    	public void setEmail(String email) {
    		this.email = email;
    	}
    
    	public String getCompanyName() {
    		return companyName;
    	}
    
    	public void setCompanyName(String companyName) {
    		this.companyName = companyName;
    	}
    
    	public Boolean getIsPowerUser() {
    		return isPowerUser;
    	}
    
    	public void setIsPowerUser(Boolean isPowerUser) {
    		this.isPowerUser = isPowerUser;
    	}
    
    	public Team getTeam() {
    		return team;
    	}
    
    	public void setTeam(Team team) {
    		this.team = team;
    	}
    
    	@Override
    	public String toString() {
    
    		return super.toString() + " name = " + firstName + " " + lastName
    				+ " id = " + id;
    	}
    
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result
    				+ ((firstName == null) ? 0 : firstName.hashCode());
    		result = prime * result + ((id == null) ? 0 : id.hashCode());
    		result = prime * result
    				+ ((lastName == null) ? 0 : lastName.hashCode());
    		return result;
    	}
    
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		Person other = (Person) obj;
    		if (firstName == null) {
    			if (other.firstName != null)
    				return false;
    		} else if (!firstName.equals(other.firstName))
    			return false;
    		if (id == null) {
    			if (other.id != null)
    				return false;
    		} else if (!id.equals(other.id))
    			return false;
    		if (lastName == null) {
    			if (other.lastName != null)
    				return false;
    		} else if (!lastName.equals(other.lastName))
    			return false;
    		return true;
    	}
    
    }

  3. #3
    Join Date
    Feb 2010
    Location
    India
    Posts
    14

    Default

    Team Class

    Code:
    @Entity
    @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
    public class Team implements Serializable{
    	private static final long serialVersionUID = -3235566821268502454L;
    	
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	private Long id;
    	
    	@Column
    	@NotEmpty
    	@NotNull
    	private String name;
    	
    	public Long getId() {
    		return id;
    	}
    	public void setId(Long id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + ((id == null) ? 0 : id.hashCode());
    		result = prime * result + ((name == null) ? 0 : name.hashCode());
    		return result;
    	}
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		Team other = (Team) obj;
    		if (id == null) {
    			if (other.id != null)
    				return false;
    		} else if (!id.equals(other.id))
    			return false;
    		if (name == null) {
    			if (other.name != null)
    				return false;
    		} else if (!name.equals(other.name))
    			return false;
    		return true;
    	}
    	@Override
    	public String toString() {
    		return "Team [name=" + name + "]";
    	}
    }

  4. #4
    Join Date
    Feb 2010
    Location
    India
    Posts
    14

    Default

    This is the log when I run the unit test cases

    Code:
    INFO : org.hibernate.tool.hbm2ddl.SchemaUpdate - Running hbm2ddl schema update
    INFO : org.hibernate.tool.hbm2ddl.SchemaUpdate - fetching database metadata
    INFO : org.hibernate.tool.hbm2ddl.SchemaUpdate - updating schema
    INFO : org.hibernate.validator.engine.resolver.DefaultTraversableResolver - Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
    INFO : org.hibernate.tool.hbm2ddl.TableMetadata - table found: crm4.person
    INFO : org.hibernate.tool.hbm2ddl.TableMetadata - columns: [id, username, email, lastname, firstname, password, companyname, ispoweruser]
    INFO : org.hibernate.tool.hbm2ddl.TableMetadata - foreign keys: [fk8e488775db099ed4, fk8e48877545c5d646]
    INFO : org.hibernate.tool.hbm2ddl.TableMetadata - indexes: [fk8e488775db099ed4, username, fk8e48877545c5d646, primary]
    INFO : org.hibernate.tool.hbm2ddl.TableMetadata - table found: crm4.personrole
    INFO : org.hibernate.tool.hbm2ddl.TableMetadata - columns: [id, emailpermission, name]
    INFO : org.hibernate.tool.hbm2ddl.TableMetadata - foreign keys: []
    INFO : org.hibernate.tool.hbm2ddl.TableMetadata - indexes: [primary]
    INFO : org.hibernate.tool.hbm2ddl.TableMetadata - table found: crm4.team
    INFO : org.hibernate.tool.hbm2ddl.TableMetadata - columns: [id, name]
    INFO : org.hibernate.tool.hbm2ddl.TableMetadata - foreign keys: []
    INFO : org.hibernate.tool.hbm2ddl.TableMetadata - indexes: [primary]
    INFO : org.hibernate.tool.hbm2ddl.SchemaUpdate - schema update complete
    INFO : org.springframework.test.context.transaction.TransactionalTestExecutionListener - Began transaction (1): transaction manager [org.springframework.orm.jpa.JpaTransactionManager@1ef2fe6]; rollback [true]
    Hibernate: insert into Team (name) values (?)
    ***10
    Hibernate: insert into Person (companyName, email, firstName, isPowerUser, lastName, password, team_id, userName) values (?, ?, ?, ?, ?, ?, ?, ?)
    WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: 1452, SQLState: 23000
    ERROR: org.hibernate.util.JDBCExceptionReporter - Cannot add or update a child row: a foreign key constraint fails (`crm4`.`person`, CONSTRAINT `FK8E48877545C5D646` FOREIGN KEY (`id`) REFERENCES `team` (`id`))
    INFO : org.springframework.test.context.transaction.TransactionalTestExecutionListener - Rolled back transaction after test execution for test context [[TestContext@182dc66 testClass = PersonControllerTest, locations = array<String>['classpath:/test-context.xml'], testInstance = org.grassfield.crm4.controller.PersonControllerTest@53c1d4, testMethod = shouldReturnPersonListView@PersonControllerTest, testException = javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [org.grassfield.crm4.model.Person]]]
    INFO : org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@2589c3: startup date [Thu Nov 29 10:20:49 GMT 2012]; root of context hierarchy
    INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@c03da9: defining beans [placeholderConfig,entityManagerFactory,dataSource,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,dataInitializer,homeController,personController,personDao,personRoleDao,teamDao,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor]; root of factory hierarchy
    INFO : org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'acme'
    INFO : org.hibernate.impl.SessionFactoryImpl - closing
    The new Team object is saved, I could get the ID as 10. But it fails when it updates the Person record.
    Any clues?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •