mercredi 8 avril 2015

Exception when save in a JPA definition with Spring Boot + Spring data JPA

I have a problem with a JPA definition using Spring Boot + Spring Data JPA, I have defined the next entities:


CommerceCnae.java



@Entity
public class CommerceCnae implements Serializable {

private static final long serialVersionUID = -5071526484444404982L;

@Id
private String name;

@OneToMany( mappedBy = "commerce", cascade = CascadeType.ALL, orphanRemoval = true)
private List<AddressCnae> addresses;

@OneToMany( mappedBy = "commerceCategory", cascade = CascadeType.ALL, orphanRemoval = true)
private List<CategoryCnae> categories;

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the addresses
*/
public List<AddressCnae> getAddresses() {
return addresses;
}

/**
* @param addresses
* the addresses to set
*/
public void setAddresses(List<AddressCnae> addresses) {
this.addresses = addresses;
}

/**
* @return the categories
*/
public List<CategoryCnae> getCategories() {
return categories;
}

/**
* @param categories
* the categories to set
*/
public void setCategories(List<CategoryCnae> categories) {
this.categories = categories;
}

}


CategoryCnae.java



@Entity
public class CategoryCnae implements Serializable {

/**
*
*/
private static final long serialVersionUID = 3889422753757007724L;
@Id
private String id;
private String description;

@ManyToOne
@JoinColumn(name = "name", nullable = false)
private CommerceCnae commerceCategory;

/**
* @return the id
*/
public String getId() {
return id;
}

/**
* @param id
* the id to set
*/
public void setId(String id) {
this.id = id;
}

/**
* @return the description
*/
public String getDescription() {
return description;
}

/**
* @param description
* the description to set
*/
public void setDescription(String description) {
this.description = description;
}

/**
* @return the commerceCategory
*/
public CommerceCnae getCommerceCategory() {
return commerceCategory;
}

/**
* @param commerceCategory
* the commerceCategory to set
*/
public void setCommerceCategory(CommerceCnae commerceCategory) {
this.commerceCategory = commerceCategory;
}

}


AddressCnae.java



@Entity
public class AddressCnae implements Serializable {
/**
*
*/
private static final long serialVersionUID = 7387779987115612860L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long addressCnaeId = 0;

private String city;
private String address;

@ManyToOne
@JoinColumn(name = "name", nullable = false)
private CommerceCnae commerce;

/**
* @return the city
*/
public String getCity() {
return city;
}

/**
* @param city
* the city to set
*/
public void setCity(String city) {
this.city = city;
}

/**
* @return the address
*/
public String getAddress() {
return address;
}

/**
* @param address
* the address to set
*/
public void setAddress(String address) {
this.address = address;
}

@Override
public boolean equals(Object obj) {
if ((obj instanceof AddressCnae)) {
AddressCnae addressCnaeObj = (AddressCnae) obj;
return (addressCnaeObj.getAddress().equals(this.getAddress()) && addressCnaeObj.getCity().equals(this.city));
} else {
return false;

}
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + this.address.hashCode() + this.city.hashCode();
return result;
}

/**
* @return the commerce
*/
public CommerceCnae getCommerce() {
return commerce;
}

/**
* @param commerce
* the commerce to set
*/
public void setCommerce(CommerceCnae commerce) {
this.commerce = commerce;
}

/**
* @return the addressCnaeId
*/
public long getAddressCnaeId() {
return addressCnaeId;
}

/**
* @param addressCnaeId
* the addressCnaeId to set
*/
public void setAddressCnaeId(long addressCnaeId) {
this.addressCnaeId = addressCnaeId;
}

}


And here is my repository:



@Repository
public interface CommerceRepository extends JpaRepository<CommerceCnae, String> {

}


But when save I have the next exception:



2015-04-08 11:22:31.320 DEBUG 18145 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : could not execute statement [n/a]

java.sql.SQLException: Field 'commerce' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)


Is a bi dirrectional relation, and my configuration is the next:



#DATASOURCE CONFIGURATION

spring.datasource.url=jdbc:mysql://XXXXX/somthing
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=XXXXX
spring.datasource.password=XXXXX
spring.datasource.continueOnError=false
spring.datasource.max-active=5

#JPA CONFIGURATION
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.hbm2ddl.auto=create-drop


What is wrong?


Thanks for your help!


Aucun commentaire:

Enregistrer un commentaire