I went back to the beginning, and redid everything, and I have included all of the source for all the files. I have tried the suggestions, and I am at a loss, so any suggestions would be greatly appreciated.
All Java files:
------------BEGIN HibernateUtil.java ---------------
package com.example.apps.reports.dao;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
}catch(Throwable t){
System.err.println("Initial SessionFactory creation failed " + t);
throw new ExceptionInInitializerError(t);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
------------END HibernateUtil.java ---------------
------------BEGIN DataService.java ---------------
package com.example.apps.reports.service;
import org.hibernate.Session;
import com.example.apps.reports.dao.HibernateUtil;
import com.example.apps.reports.vo.EmailAddress;
import com.example.apps.reports.vo.UserVo;
import com.example.apps.reports.vo.ValueObject;
public class DataService {
public static void saveItem(ValueObject vo){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.saveOrUpdate(vo);
session.getTransaction().commit();
}
public static void main(String[] args) {
UserVo u = null;
EmailAddress e = null;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
//This should pull back UserId 4 First Name Test4 and emailId 2 with
Test4@here.com
u = (UserVo) session.createQuery("from UserVo where userId = ?").setInteger(0, 4).uniqueResult();
session.getTransaction().commit();
System.out.println(u.getFirstName());
//EMAIL is null
System.out.println(u.getEmail().getEmailAddress());
}
}
------------END DataService.java ---------------
------------BEGIN ValueObject.java ---------------
package com.example.apps.reports.vo;
public class ValueObject {
}
------------END ValueObject.java ---------------
------------BEGIN UserVo.java --------------
package com.example.apps.reports.vo;
public class UserVo extends ValueObject{
private int userId;
private String firstName;
private EmailAddress email;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public EmailAddress getEmail() {
return email;
}
public void setEmail(EmailAddress email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
------------END UserVo.java --------------
------------BEGIN EmailAddress.java --------------
package com.example.apps.reports.vo;
public class EmailAddress extends ValueObject{
private int emailId;
private UserVo user;
private String emailAddress;
public int getEmailId() {
return emailId;
}
public void setEmailId(int emailId) {
this.emailId = emailId;
}
public UserVo getUser() {
return user;
}
public void setUser(UserVo user) {
this.user = user;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
------------END EmailAddress.java --------------
HIBERNATE CONFIG AND MAPPING:
------------BEGIN hibernate.cfg.xml --------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/testhibernate</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property> -->
<mapping resource="com/example/apps/reports/vo/mappings/UserVo.hbm.xml"/>
<mapping resource="com/example/apps/reports/vo/mappings/EmailAddress.hbm.xml"/>
</session-factory>
</hibernate-configuration>
------------END hibernate.cfg.xml --------------
------------BEGIN UserVo.hbm.xml --------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.example.apps.reports.vo.UserVo" table="users">
<id column="user_id" name="userId">
<generator class="native"/>
</id>
<property name="firstName" column="first_name"/>
<one-to-one cascade="all" name="email" class="com.example.apps.reports.vo.EmailAddress"/>
</class>
</hibernate-mapping>
------------END UserVo.hbm.xml --------------
------------BEGIN EmailAddress.hbm.xml --------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.example.apps.reports.vo.EmailAddress" table="email">
<id name="emailId" column="email_id">
<generator class="native"/>
</id>
<one-to-one name="user" foreign-key="user_id" class="com.example.apps.reports.vo.UserVo" />
<property name="emailAddress" column="email_address" type="java.lang.String"/>
</class>
</hibernate-mapping>
------------END EmailAddress.hbm.xml --------------
------------BEGIN create_mysql.sql --------------
drop database if exists testhibernate;
create database testHibernate;
use testHibernate;
drop table if exists users;
create table users(
user_id int not null auto_increment,
first_name varchar(30),
created_date datetime,
modified_date datetime,
created_by varchar(30),
modified_by varchar(30),
primary key(user_id)
);
drop trigger if exists aduit_users_insert;
DELIMITER //
create trigger aduit_users_insert before insert on users
for each row begin
set NEW.created_date = now();
if NEW.created_by is null then
SET NEW.created_by = 'SYSTEM';
end if;
end;//
drop trigger if exists audit_users_update//
create trigger audit_users_update before update on users
for each row
begin
set NEW.modified_date = now();
if NEW.modified_by is null then
SET NEW.modified_by = 'SYSTEM';
end if;
end; //
DELIMITER ;
insert into users (first_name) values('Test1');
insert into users (first_name) values('Test2');
insert into users (first_name) values('Test3');
insert into users (first_name) values('Test4');
drop table if exists email;
create table email(
email_id int not null auto_increment,
user_id int,
email_address varchar(70),
created_date datetime,
modified_date datetime,
created_by varchar(30),
modified_by varchar(30),
foreign key (user_id) references users(user_id),
primary key(email_id)
);
drop trigger if exists aduit_email_insert;
DELIMITER //
create trigger aduit_email_insert before insert on email
for each row begin
set NEW.created_date = now();
if NEW.created_by is null then
SET NEW.created_by = 'SYSTEM';
end if;
end;//
drop trigger if exists audit_email_update//
create trigger audit_email_update before update on email
for each row
begin
set NEW.modified_date = now();
if NEW.modified_by is null then
SET NEW.modified_by = 'SYSTEM';
end if;
end; //
DELIMITER ;
insert into email (user_id, email_address) values(1,
'Test1@here.com');
insert into email (email_address) values(
'Test4@here.com');
------------END create_mysql.sql --------------