Hello.

Just started using appfuse and created the models Category and Item.

When I try to test the creation of items in my CategoryDaoTest I get the
following error:
could not insert: [Item]; uncategorized SQLException for SQL [insert into
item (amount, created_date, last_edited) values (?, ?, ?)]; SQL state
[HY000]; error code [1364]; Field 'category_id' doesn't have a default
value; nested exception is java.sql.SQLException: Field 'category_id'
doesn't have a default value

This is how my test looks like:
--------------------------------------------
..
public void testCreateCategoryItems() {
Category category = new Category();
category.setName("test");

Item item = new Item();
item.setAmount(100.00);
category.addItem(item);
                
category = categoryDao.save(category);
flush();
                
Category storedCategory = categoryDao.get(category.getCategoryId());
assertNotNull(storedCategory);
assertNotNull(storedCategory.getItems());
}
..
--------------------------------------------

My annotations in the Category model:
--------------------------------------------
..
@OneToMany( 
  cascade = {CascadeType.ALL},
  fetch = FetchType.EAGER
)
@JoinColumn(name="category_id")
public List<Item> items = new ArrayList<Items>();
..
public void addItem(Item item) {
  if ( item == null ) throw new IllegalArgumentException("Cannot add an
empty item");
    item.setCategory(this);
    this.items.add(item);
}
..
--------------------------------------------

My annotations in the Item model:
--------------------------------------------
..
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, optional =
true)
@JoinColumn(name="category_id", updatable = false, insertable = false,
nullable = false )
private Category category;
..
--------------------------------------------

As far as I can tell the mappings should be correct, so I am a bit stuck on
this. Anyone who can see any obvious errors in my code?

Thanks :)
-- Al
-- 
View this message in context: 
http://www.nabble.com/SQL-does-not-store-FK-when-implementing-OneToMany-relationship.-tp18560099s2369p18560099.html
Sent from the AppFuse - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to