stinkyPete
stinkyPete

Reputation: 1

MyBatis mapping database and objects together

I am having some trouble with struts and inserting a record into my database

Database is like this

Here are the maps:

<mapper namespace="Employees">
<resultMap id="employeeMap" type="employee">
    <id column="employeeID" property="employeeID" />
    <result column="employeeNum" property="employeeNum" />
    <result column="username" property="userName" />
    <result column="firstName" property="firstName" />
    <result column="lastName" property="lastName" />


    <association property="address" column="addressID" resultMap="addressMap" />
</resultMap>

<resultMap id="addressMap" type="address">
    <id column="addressID" property="id" />
    <result column="addressLine1" property="addressLine1" />
    <result column="addressLine2" property="addressLine2" />
    <result column="city" property="city" />
    <result column="postcode" property="postcode" />
    <result column="country" property="country" />
</resultMap>

This is the Employee object:

public class Employee{

private static final long serialVersionUID = 1L;

private int employeeID;
private String employeeNum;
private String firstName;
private String lastName;

How should my table look with regards to storing the relationship above?

At the minute the Employees table looks like: employeeID employeeNum firstName lastName addressID

And I keep getting an error like "addressID doesn't have a default value" Is everything laid out the way it should be or am I missing something?..

Upvotes: 0

Views: 1046

Answers (1)

jddsantaella
jddsantaella

Reputation: 3687

You are trying to insert an employee but you are missing addressID and this attribute is needed in order to insert it. I guess that, because you are showing us the mapping, not the insert query.

When you create a table in database, you can choose a "default value" for a column. When you insert a register and miss a column, its default value is used. You miss addressID and there is not a default value of it neither, so you get an error.

So, create a address attribute for your Employee and use its id in the insert query.

Upvotes: 1

Related Questions