Posts filed under 'Spring'

new enchancments to EJB 3 in the form of Singleton pattern

The article about the futher enhancments in EJB 3 specific spectrum by Reha Rahman highlits the new dimensions the EJB 3 specification is heading…added pattern of Singletons…you could send your feedback about the planned new features of EJB 3.1…give your feedback….also big influence in the whole was the Oracle’s Bea aqusition…the the Spring supporters are now one mammunt….read more about Rod Jhonsson’s feelings about this merge. Planning to attend some major Java seminars in Europe…why not to attend Java Sympohisum held In Prague during next Summer -

 

Add comment January 26th, 2008

JPA and Spring Sample Application

Hi,

As I started to play with the Spring 2.0 and JPA technologies I came up with a simplistic data models and samples of how to create Entities using Hibernate EntityManager in the background and how to access them with Spring DAO classes implementing JpaTemplate way of handling with the EntityManager. In addition, a AbstractJpaTest class was created to peform Junit test againts Spring DAO layer classes.

Database initialization sql script:
drop table Contract;
drop table ContractPerson;
drop table Company;
drop table Adress;

drop index ContractHost;
drop index ContractQuest;
drop index ContractAdress;
drop SEQUENCE adress_seq;
drop SEQUENCE company_seq;
drop SEQUENCE contractperson_seq;
drop SEQUENCE contract_seq;

drop table Contract;
drop table ContractPerson;
drop table Company;
drop table Adress;

CREATE SEQUENCE adress_seq START 1000;
CREATE SEQUENCE company_seq START 1000;
CREATE SEQUENCE contractperson_seq START 1000;
CREATE SEQUENCE contract_seq START 1000;

create table Adress (
Adress_id int not null DEFAULT NEXTVAL(’adress_seq’),
status varchar(2) null,
addr1 varchar(80) not null,
addr2 varchar(40) null,
city varchar(80) not null,
state varchar(80) not null,
zip varchar(20) not null,
country varchar(20) not null,
phone varchar(80) not null,
constraint pk_adress primary key (Adress_id)
);
create table Company (
Company_id int not null DEFAULT NEXTVAL(’company_seq’),
company_adress_id int not null,
name varchar(40) not null,
constraint pk_company primary key (Company_id),
constraint fk_company_adress foreign key (company_adress_id ) references Adress (Adress_id)
);
create table ContractPerson
(
ContractPerson_id int not null DEFAULT NEXTVAL(’contractperson_seq’),
person_address_id int not null,
company_id int not null,
email varchar(80) not null,
firstname varchar(80) not null,
lastname varchar(80) not null,
pincode varchar(15) not null,
constraint pk_contractperson primary key (ContractPerson_id) ,
constraint fk_adress foreign key (person_address_id) references Adress (Adress_id),
constraint fk_company_id foreign key (company_id) references Company (Company_id)
);
create table Contract (
contract_id int not null DEFAULT NEXTVAL(’contract_seq’),
host_persion_id int not null,
quest_persion_id int not null,
contract_adress_id int not null,
status varchar(2) not null,
created_date date not null,
document1_name varchar(50) null,
document2_name varchar(50) null,
document1_path varchar(50) null,
document2_path varchar(50) null,

constraint pk_contract primary key (contract_id) ,
constraint fk_host_person foreign key (host_persion_id) references ContractPerson (ContractPerson_id),
constraint fk_quest_person foreign key (quest_persion_id) references ContractPerson (ContractPerson_id),
constraint fk_contract_adress foreign key (contract_adress_id) references Adress (Adress_id)
);
create index ContractHost on Contract (host_persion_id );
create index ContractQuest on Contract (quest_persion_id );
create index ContractAdress on Contract (contract_adress_id );

ALTER TABLE adress OWNER TO user;
GRANT ALL ON TABLE adress TO user;
GRANT ALL ON TABLE adress TO contactdb;

ALTER TABLE company OWNER TO user;
GRANT ALL ON TABLE company TO user;
GRANT ALL ON TABLE company TO contact;

ALTER TABLE contract OWNER TO user;
GRANT ALL ON TABLE contract TO user;
GRANT ALL ON TABLE contract TO contact;

ALTER TABLE contractperson OWNER TO user;
GRANT ALL ON TABLE contractperson TO contact;
GRANT ALL ON TABLE contractperson TO user;

ALTER TABLE adress_seq OWNER TO user;
GRANT ALL ON TABLE adress_seq TO contact;
GRANT ALL ON TABLE adress_seq TO user;

ALTER TABLE company_seq OWNER TO user;
GRANT ALL ON TABLE company_seq TO contact;
GRANT ALL ON TABLE company_seq TO user;

ALTER TABLE contract_seq OWNER TO user;
GRANT ALL ON TABLE contract_seq TO contact;
GRANT ALL ON TABLE contract_seq TO user;

ALTER TABLE contractperson_seq OWNER TO user;
GRANT ALL ON TABLE contractperson_seq TO contact;
GRANT ALL ON TABLE contractperson_seq TO user;

commit;

Beans configuration file:

Wieard issue that I’m not able to paste spring beans configuration file context.
Adress Entity:

package fi.passiba.contractdb.persistence;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.SequenceGenerator;
import javax.persistence.Id;

@Entity
public class Adress implements Serializable {
@Id
@Column(name=”adress_id”)
@GeneratedValue (strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name=”USER_SEQUENCE_GENERATOR_ADDRESS”,sequenceName=”adress_seq”)

private int adressId;

@Column(name=”country”)
private String country;

@Column(name=”state”)
private String state;

@Column(name=”status”)
private String status;

@Column(name=”zip”)
private String zip;

@Column(name=”phone”)
private String phone;

@Column(name=”addr2″)
private String addr2;

@Column(name=”addr1″)
private String addr1;

@Column(name=”city”)
private String city;

private static final long serialVersionUID = 1L;

public Adress() {
super();
}

public int getAdressId() {
return this.adressId;
}

public String getCountry() {
return this.country;
}

public void setCountry(String country) {
this.country = country;
}

public String getState() {
return this.state;
}

public void setState(String state) {
this.state = state;
}

public String getStatus() {
return this.status;
}

public void setStatus(String status) {
this.status = status;
}

public String getZip() {
return this.zip;
}

public void setZip(String zip) {
this.zip = zip;
}

public String getPhone() {
return this.phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getAddr2() {
return this.addr2;
}

public void setAddr2(String addr2) {
this.addr2 = addr2;
}

public String getAddr1() {
return this.addr1;
}

public void setAddr1(String addr1) {
this.addr1 = addr1;
}

public String getCity() {
return this.city;
}

public void setCity(String city) {
this.city = city;
}

}

Company Entity:
package fi.passiba.contractdb.persistence;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;

@Entity
public class Company implements Serializable {
@Id
@Column(name=”company_id”)
@GeneratedValue (strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name=”USER_SEQUENCE_GENERATOR_COMPANY”,sequenceName=”company_seq”)

private int companyId;

@Column(name=”name”)
private String name;

@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name = “company_adress_id”, referencedColumnName = “Adress_id”)
private Adress address_id;

private static final long serialVersionUID = 1L;

public Company() {
super();
}

public int getCompanyId() {
return this.companyId;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public Adress getAdress() {
return this.address_id;
}

public Adress setAdress(Adress adress) {
return this.address_id = adress;
}

}

Contract Entity:

package fi.passiba.contractdb.persistence;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;

@Entity
@NamedQueries( {
@NamedQuery(name = “Contract.findContractByDocument1Name”, query = “SELECT c FROM Contract AS c WHERE c.document1Name LIKE ?1″),
@NamedQuery(name = “Contract.findContractByDocument2Name”, query = “SELECT c FROM Contract AS c WHERE c.document2Name LIKE ?1″),
@NamedQuery(name = “Contract.findContractByQuest”, query = “SELECT c FROM Contract AS c JOIN c.questPersionId AS p WHERE p.lastname = ?1″),
@NamedQuery(name = “Contract.findContractByHost”, query = “SELECT c FROM Contract AS c JOIN c.hostPersionId AS p WHERE p.lastname = ?1″),
@NamedQuery(name = “Contract.findContractByStatus”, query = “SELECT c FROM Contract AS c WHERE c.status = ?1″),
@NamedQuery(name = “Contract.findContractByDate”, query = “SELECT c FROM Contract AS c WHERE c.createdDate > :currentDate”)

}
)
public class Contract implements Serializable {
@Id
@Column(name=”contract_id”)
@GeneratedValue (strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name=”USER_SEQUENCE_GENERATOR_CONTRACT”,sequenceName=”contract_seq”)

private int contractId;

@Column(name=”status”)
private String status;

@Column(name=”document2_name”)
private String document2Name;

@Column(name=”document2_path”)
private String document2Path;

@Column(name=”document1_name”)
private String document1Name;

@Column(name=”document1_path”)
private String document1Path;

@Column(name = “created_date”)
private Timestamp createdDate;

@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name = “host_persion_id”, referencedColumnName = “ContractPerson_id”)
private Contractperson hostPersionId;

@OneToOne(cascade = CascadeType.ALL , fetch=FetchType.LAZY)
@JoinColumn(name = “quest_persion_id”, referencedColumnName = “ContractPerson_id”)
private Contractperson questPersionId;

@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name = “contract_adress_id”, referencedColumnName = “Adress_id”)
private Adress contract_address_id;

private static final long serialVersionUID = 1L;

public Contract() {
super();
}

public int getContractId() {
return this.contractId;
}

public String getStatus() {
return this.status;
}

public void setStatus(String status) {
this.status = status;
}

public String getDocument2Name() {
return this.document2Name;
}

public void setDocument2Name(String document2Name) {
this.document2Name = document2Name;
}

public String getDocument2Path() {
return this.document2Path;
}

public void setDocument2Path(String document2Path) {
this.document2Path = document2Path;
}

public String getDocument1Name() {
return this.document1Name;
}

public void setDocument1Name(String document1Name) {
this.document1Name = document1Name;
}

public String getDocument1Path() {
return this.document1Path;
}

public void setDocument1Path(String document1Path) {
this.document1Path = document1Path;
}

public Contractperson getHostPersionId() {
return this.hostPersionId;
}

public void setHostPersionId(Contractperson hostPersionId) {
this.hostPersionId = hostPersionId;
}

public Contractperson getQuestPersionId() {
return this.questPersionId;
}

public void setQuestPersionId(Contractperson questPersionId) {
this.questPersionId = questPersionId;
}

public Adress getContractAdress() {
return this.contract_address_id;
}

public void setContractAdress(Adress contractAdress) {
contract_address_id = contractAdress;
}
public Timestamp getCreatedDate() {
return createdDate;
}

public void setCreatedDate(Timestamp createdDate) {
this.createdDate = createdDate;
}

}

Contractperson Entity:

package fi.passiba.contractdb.persistence;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;

@Entity

@NamedQueries( {
@NamedQuery(name = “Contractperson.findContractPersonByCompany”, query = “SELECT p FROM Contractperson AS p JOIN p.companyId AS c WHERE c.name = ?1″)
}
)
public class Contractperson implements Serializable {
@Id
@Column(name=”contractperson_id”)
@GeneratedValue (strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name=”USER_SEQUENCE_GENERATOR_CONTRACTPERSON”,sequenceName=”contractperson_seq”)

private int contractpersonId;

@Column(name=”email”)
private String email;

@Column(name=”lastname”)
private String lastname;

@Column(name=”firstname”)
private String firstname;

@Column(name=”pincode”)
private String pincode;

@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name = “company_id”, referencedColumnName = “Company_id”)
private Company companyId;

@OneToOne(cascade = CascadeType.ALL,fetch=FetchType.LAZY)
@JoinColumn(name = “person_address_id”, referencedColumnName = “Adress_id”)

//@Column(name=”person_address_id”)
private Adress personAddressId;

private static final long serialVersionUID = 1L;

public Contractperson() {
super();
}

public int getContractpersonId() {
return this.contractpersonId;
}

public String getEmail() {
return this.email;
}

public void setEmail(String email) {
this.email = email;
}

public String getLastname() {
return this.lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getFirstname() {
return this.firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getPincode() {
return this.pincode;
}

public void setPincode(String pincode) {
this.pincode = pincode;
}

public Company getCompanyId() {
return this.companyId;
}

public void setCompanyId(Company companyId) {
this.companyId = companyId;
}

public Adress getPersonAddressId() {
return this.personAddressId;
}

public void setPersonAddressId(Adress personAddressId) {
this.personAddressId = personAddressId;
}

}

DAO Interface class:

package fi.passiba.contractdb.persistence.dao;

import fi.passiba.contractdb.persistence.Contract;
import fi.passiba.contractdb.persistence.Contractperson;

import java.util.Date;
import java.util.List;

public interface ContractEAO{

public Contract findByContacId(int contactId);

public List findByDocumentName1(String documentName);
public List findByDocumentName2(String documentName);

public List getContactByDate(Date currentDate);

public List getContactByStatus(String status);

public List getContactByHostName(String hostName);

public List getContactByQuestName(String questName);

public List getContactByCompanyName(String questName);

public Contract createContract(Contract contract);

public Contract updateContract(Contract contract);

public void deleteContract(Contract contract);
}

Spring DAO class using JpaTemplate to access EntityManager

package fi.passiba.contractdb.persistence.dao;

import fi.passiba.contractdb.persistence.Contract;
import fi.passiba.contractdb.persistence.Contractperson;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import java.util.Date;
import java.util.List;

public class ContractSpringEAO extends JpaDaoSupport implements ContractEAO {

public Contract findByContacId(int contactId) {
return getJpaTemplate().find(Contract.class, contactId);
}

public List findByDocumentName1(String documentName) {

String sql=”Select c FROM Contract c WHERE c.document1Name LIKE ?1 order by c.contractId”;
return (List)getJpaTemplate().find(sql,documentName);
}

public List findByDocumentName2(String documentName) {
return (List)getJpaTemplate().find(”Select c FROM Contract c WHERE c.document2Name LIKE ?1 order by c.contractId”,documentName);
}

public List getContactByCompanyName(String companyName) {
return (List)getJpaTemplate().find(”SELECT p FROM Contractperson p JOIN p.companyId as c WHERE c.name = ?1″,companyName);
}

public List getContactByDate(Date currentDate) {
return (List)getJpaTemplate().find(”SELECT c FROM Contract c WHERE c.createdDate > ?1″,currentDate);
}

public List getContactByHostName(String hostName) {
return (List)getJpaTemplate().find(”SELECT c FROM Contract c JOIN c.hostPersionId AS p WHERE p.lastname = ?1″,hostName);
}

public List getContactByQuestName(String questName) {
return (List)getJpaTemplate().find(”SELECT c FROM Contract c JOIN c.questPersionId AS p WHERE p.lastname = ?1″,questName);
}

public List getContactByStatus(String status) {
return (List)getJpaTemplate().find(”SELECT c FROM Contract c WHERE c.status = ?1″,status);
}

public Contract createContract(Contract contract) {
getJpaTemplate().persist(contract);
return contract;
}

public void deleteContract(Contract contract) {
getJpaTemplate().remove(contract);

}

public Contract updateContract(Contract contract) {
getJpaTemplate().merge(contract);
return contract;
}

}

Persistance.xml

fi.passiba.contractdb.persistence.Adress
fi.passiba.contractdb.persistence.Contract
fi.passiba.contractdb.persistence.Company
fi.passiba.contractdb.persistence.Contractperson
Test class to test Spring DAO class

fi.passiba.contractdb.persistence.Adress
fi.passiba.contractdb.persistence.Contract
fi.passiba.contractdb.persistence.Company
fi.passiba.contractdb.persistence.Contractperson
Junit test class extending abtract org.springframework.test.jpa.AbstractJpaTests-class

package test.passiba.contractdb;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.test.jpa.AbstractJpaTests;

import fi.passiba.contractdb.buslogic.ContractService;
import fi.passiba.contractdb.persistence.Adress;
import fi.passiba.contractdb.persistence.Company;
import fi.passiba.contractdb.persistence.Contract;
import fi.passiba.contractdb.persistence.Contractperson;
public class ContractdbBusinessServiceImplTest extends AbstractJpaTests {

private ContractService empBusiness;

private Timestamp findByDate=null;

private static int id= 0;

public void setContractService(ContractService empBusiness) {
this.empBusiness = empBusiness;
}
protected String[] getConfigLocations() {
return new String[] { “classpath:/test/passiba/contractdb/contract-test-service.xml” };
}
protected void onSetUpInTransaction() throws Exception {
Contract contract2= new Contract();

findByDate= new Timestamp(System.currentTimeMillis());

contract2.setCreatedDate(findByDate);

Adress company_address1= new Adress();
company_address1.setAddr1(”TestiBulevardi 1″);
company_address1.setAddr2(”PL 564″);
company_address1.setCity(”Helsinki”);
company_address1.setCountry(”Suomi”);
company_address1.setPhone(”0976443333″);
company_address1.setState(”Uusimaa”);
company_address1.setZip(”00100″);
company_address1.setStatus(”1″);

Company company1 = new Company();
company1.setAdress(company_address1);
company1.setName(”Passiba Oyj”);
Adress company_address2= new Adress();

company_address2.setAddr1(”Normandiankuja 1″);
company_address2.setAddr2(”PL 23423″);
company_address2.setCity(”Helsinki”);
company_address2.setCountry(”Suomi”);
company_address2.setPhone(”0923567567″);
company_address2.setState(”Uusimaa”);
company_address2.setZip(”00100″);
company_address1.setStatus(”1″);

Company company2= new Company();

company2.setAdress(company_address2);
company2.setName(”Empire Oyj”);

Contractperson person1= new Contractperson();
person1.setCompanyId(company1);
person1.setEmail(”salesman@passiba.fi”);
person1.setFirstname(”Frank”);
person1.setLastname(”Miller”);
person1.setPincode(”231284-007″);
Adress person_address1= new Adress();

person_address1.setAddr1(”Sales deadendalley 1″);
person_address1.setAddr2(”PL 23423″);
person_address1.setCity(”London”);
person_address1.setCountry(”Englanti”);
person_address1.setPhone(”0923423423″);
person_address1.setState(”London”);
person_address1.setZip(”009923″);
person_address1.setStatus(”1″);

person1.setPersonAddressId(person_address1);

Contractperson person2= new Contractperson();
person2.setCompanyId(company2);
person2.setEmail(”salesman@eniro.fi”);
person2.setFirstname(”Eve”);
person2.setLastname(”SaleAce”);
person2.setPincode(”110280-009″);
Adress person_address2= new Adress();
person_address2.setAddr1(”Roihuvuorentie 1″);
person_address2.setAddr2(”PL 223423″);
person_address2.setCity(”Helsinki”);
person_address2.setCountry(”Suomi”);
person_address2.setPhone(”023423423423″);
person_address2.setState(”Uusimaa”);
person_address2.setZip(”00199″);
person_address2.setStatus(”1″);
person2.setPersonAddressId(person_address2);

contract2.setDocument1Name(”sopumus.pdf”);
contract2.setDocument1Path(”c:/docut”);
contract2.setDocument2Name(”sopimusliite.txt”);
contract2.setDocument2Path(”c:/files”);
contract2.setHostPersionId(person1);
contract2.setQuestPersionId(person2);
contract2.setStatus(”1″);
Adress contract_address=new Adress();

contract_address.setAddr1(”So¶rnaistenkatu 1″);
contract_address.setAddr2(”PL 23423″);
contract_address.setCity(”Helsinki”);
contract_address.setCountry(”Suomi”);
contract_address.setPhone(”0982342323″);
contract_address.setState(”Uusimaa”);
contract_address.setZip(”00199″);
contract_address.setStatus(”1″);

contract2.setContractAdress( contract_address);
contract2=empBusiness.createContract(contract2);
id=contract2.getContractId();
}
protected void onTearDownInTransaction()
throws Exception
{
//empBusiness.deleteContract(empBusiness.findByContacId(id));
}

public void testFindByDocumentName1()
{
List result = empBusiness.findByDocumentName1(”sopumus.pdf”);
assertEquals(1, result.size());
Contract contract2= (Contract)result.get(0);
assertEquals(”sopumus.pdf”,contract2.getDocument1Name());
}
public void testFindByDocumentName2()
{
List result = empBusiness.findByDocumentName2(”sopimusliite.txt”);
assertEquals(1, result.size());
Contract contract2= (Contract)result.get(0);
assertEquals(”sopimusliite.txt”,contract2.getDocument2Name());
}

public void testGetContactByDate()
{
List result = empBusiness.getContactByDate(findByDate);
assertEquals(1, result.size());

Contract contract2= (Contract)result.get(0);

assertEquals(findByDate,contract2.getCreatedDate());

}
public void testGetContactByStatus()
{
List result = empBusiness.getContactByStatus(”1″);
assertEquals(1, result.size());
Contract contract2= (Contract)result.get(0);
assertEquals(”1″,contract2.getStatus());

}

public void testGetContactByHostName()
{
List result = empBusiness.getContactByHostName(”Miller”);
assertEquals(1, result.size());
Contract contract2= (Contract)result.get(0);
assertEquals(”Miller”,contract2.getHostPersionId().getLastname());

}

public void testGetContactByQuestName(){

List result = empBusiness.getContactByQuestName(”SaleAce”);
assertEquals(1, result.size());
Contract contract2= (Contract)result.get(0);
assertEquals(”SaleAce”,contract2.getQuestPersionId().getLastname());

}

public void testGetContactByHostCompanyName(){

List result = empBusiness.getContactByCompanyName(”Passiba Oyj”);
assertEquals(1, result.size());
Contractperson contract2= (Contractperson)result.get(0);
assertEquals(”Miller”,contract2.getLastname());

}
public void testGetContactByQuestCompanyName(){

List result = empBusiness.getContactByCompanyName(”Empire Oyj”);
assertEquals(1, result.size());
Contractperson contract2= (Contractperson)result.get(0);
assertEquals(”SaleAce”,contract2.getLastname());

}

public void testInsertContract()
{
Contract contract2= new Contract();
Adress company_address1= new Adress();
company_address1.setAddr1(”TestiBulevardi 1″);
company_address1.setAddr2(”PL 564″);
company_address1.setCity(”Helsinki”);
company_address1.setCountry(”Suomi”);
company_address1.setPhone(”0976443333″);
company_address1.setState(”Uusimaa”);
company_address1.setZip(”00100″);
company_address1.setStatus(”1″);

Company company1 = new Company();
company1.setAdress(company_address1);
company1.setName(”Passiba Oyj”);
Adress company_address2= new Adress();

company_address2.setAddr1(”Normandiankuja 1″);
company_address2.setAddr2(”PL 23423″);
company_address2.setCity(”Helsinki”);
company_address2.setCountry(”Suomi”);
company_address2.setPhone(”0923567567″);
company_address2.setState(”Uusimaa”);
company_address2.setZip(”00100″);
company_address1.setStatus(”1″);

Company company2= new Company();

company2.setAdress(company_address2);
company2.setName(”Empire Oyj”);

Contractperson person1= new Contractperson();
person1.setCompanyId(company1);
person1.setEmail(”salesman@passiba.fi”);
person1.setFirstname(”Frank”);
person1.setLastname(”Miller”);

Adress person_address1= new Adress();

person_address1.setAddr1(”Sales deadendalley 1″);
person_address1.setAddr2(”PL 23423″);
person_address1.setCity(”London”);
person_address1.setCountry(”Englanti”);
person_address1.setPhone(”0923423423″);
person_address1.setState(”London”);
person_address1.setZip(”009923″);
person_address1.setStatus(”1″);

person1.setPersonAddressId(person_address1);

Contractperson person2= new Contractperson();
person2.setCompanyId(company2);
person2.setEmail(”salesman@eniro.fi”);
person2.setFirstname(”Eve”);
person2.setLastname(”SaleAce”);

Adress person_address2= new Adress();
person_address2.setAddr1(”Roihuvuorentie 1″);
person_address2.setAddr2(”PL 223423″);
person_address2.setCity(”Helsinki”);
person_address2.setCountry(”Suomi”);
person_address2.setPhone(”023423423423″);
person_address2.setState(”Uusimaa”);
person_address2.setZip(”00199″);
person_address2.setStatus(”1″);
person2.setPersonAddressId(person_address2);

contract2.setDocument1Name(”sopumus.pdf”);
contract2.setDocument1Path(”c:/docut”);
contract2.setDocument2Name(”sopimusliite.txt”);
contract2.setDocument2Path(”c:/files”);
contract2.setHostPersionId(person1);
contract2.setQuestPersionId(person2);
contract2.setStatus(”1″);
Adress contract_address=new Adress();

contract_address.setAddr1(”Sornaistenkatu 1″);
contract_address.setAddr2(”PL 23423″);
contract_address.setCity(”Helsinki”);
contract_address.setCountry(”Suomi”);
contract_address.setPhone(”0982342323″);
contract_address.setState(”Uusimaa”);
contract_address.setZip(”00199″);
contract_address.setStatus(”1″);

contract2.setContractAdress( contract_address);
contract2=empBusiness.createContract( contract2);
id=contract2.getContractId();
assertEquals(contract2.getDocument2Name(),empBusiness.findByContacId(id).getDocument2Name());
}

public void testUpdateContract(){

Contract contract=empBusiness.findByContacId(id);
contract.setDocument2Name(”tehdyt_tyotunnit.xls”);
contract=empBusiness.updateContract( contract);
assertEquals(”tehdyt_tyotunnit.xls”,empBusiness.findByContacId(id).getDocument2Name());

}
public void testDeleteContract(){

empBusiness.deleteContract(empBusiness.findByContacId(id));
assertNull(empBusiness.findByContacId(id));

}

}

download the source code from my homepage

Be sure to download hibernate Entitymanager and Hibernte core distributions from hibernate download-site .

In addition, in order to create the database schema download Postgres database and Spring framework with dependencies.

Add comment August 30th, 2007

New release of MyEclipse 6.0 - support for JPA and Spring 2.0 Integration…JPA Streaming video tutorial

Hi,

I have been tackling with issues related to JPA and Spring Integration and luckily the tools provided by MyEclipse enable a good start for developing web applications using JPA (Java Persistance API)and Spring 2.0. The only minus side was in the tool support was the support for handling relationships between entities (one-to-one,one-to-many ,many-to-many). I basically needed to take care of configuring these generated annotations to the entities.Good tutorial to get the JPA relationships terms as part of your work toolkit vocabulary is to view introductive streaming video tutorial about using the Java Persistance API by Patrick Linskey(Bea) and Mike Keith (Oracle). Good sequel to the first streaming video session is also Enterprise Development with JPA. Topics covered in this session include Abstract Entities, Data Models and Entity Caching to name few.
The good news is also that the tools support for crating skeleton Spring DAO classes to access the JPA entities. Read the article about Java Developer journal - MyEclipse Enterprise Workbench 6.0 offers Spring 2.0 ,Java 6 and JPA Support

In, addition to that take a brief moment of your time to focus on the Tutorial Writing JPA Applications by Patrick Linskey.One of the consideration of how to access Entities in JPA was to either use EntityManager directly or to use JpaTemplates to do the job for you. The Streaming video tutorial Spring and JPA by Rod Johnson and Costin Leau highlight the best practices using JPA with Spring …how to test your JPA DAO layer using AbstractJPaTest class found in springmock.jar file.

Other useful source of information for me has been the Hibernate Entitymanager specific documentation…as I focus on Hibernate as EntityManager when using JPA. Other useful tutorials has been the IBM’s Developerworks series as the following Introduction to Spring 2 and JPA.

You just need to be registered user in order to access the above mentioned tutorial. But the effort is minimal for the reward of becoming acquainted with new terms behind JPA and Spring 2.0. In addition to Spring I’ll focus on the near future to the framework Seams in the form of recently published book by Michale Yuan - Seams next-gen Web framework.

Read also IBM’s Developerwork tutorial about the usage of seams part 1,

usage of seams part2

and

usage of seams part 3

Add comment August 29th, 2007

configuration issues of Myql server,Tomcat and reading application specific properties configuration file

Hi,

This month has been turning point to something new …with the learning curve of using hibernate combined with spring framework and also wicket as front end mvc framework. At first I faced a task of repackaging the given wicket+Spring+hibernate based application web archive file to be run on top of Apache Tomcat.

Tomcat specific how-to-listing provides good ways how to configure Tomcat to meet your specific needs.

First phase was to remove any Jetty specific jar files and other Servlet-api specific jar-files in given ant-build file. The ant specific section exclude is written false way ekslude in order to be displayed on this blog. The exclude word would leave it hidden.

webxml=”${src.web.dir}/WEB-INF/web.xml”
ekslude=”**/package.html”>

<ekslude name=”org.mortbay.*” />
<
ekslude name=”jasper*.jar” />


<
ekslude name=”hibernate/antlr-2.7.6.jar”/>
<
ekslude name=”hibernate/asm-attrs.jar” />
<
ekslude name=”hibernate/c3p0-0.9.1.jar”/>

<ekslude name=”hibernate/ehcache-1.2.3.jar” />
<
ekslude name=”hibernate/hibernate3.jar” />
<
ekslude name=”hibernate/jaxen-1.1-beta-7.jar” />
<
ekslude name=”hibernate/jdbc2_0-stdext.jar” />
<
ekslude name=”hibernate/jta.jar” />
<
ekslude name=”hibernate-annotations/ejb3-persistence.jar” />
<
ekslude name=”hibernate-annotations/hibernate-annotations.jar” />
<
ekslude name=”hibernate-annotations/hibernate-commons-annotations.jar” />

After this was done it was time to tackle with issue of not finding hibernate annotation specific jar-file and hibernate specific jar files….they were located in lib\hibernate-annotations and lib\hibernate and subdirectories. This resulted the Spring Bean instanziation Exception
Instantiation of bean failed; nested exception is

org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class
[org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]:
Constructor threw exception; nested exception is
java.lang.NoClassDefFoundError:
org/hibernate/cfg/AnnotationConfiguration
Caused by: org.springframework.beans.BeanInstantiationException: Could
not instantiate bean class
[org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]:
Constructor threw exception; nested exception is
java.lang.NoClassDefFoundError:
org/hibernate/cfg/AnnotationConfiguration
Caused by: java.lang.NoClassDefFoundError:
org/hibernate/cfg/AnnotationConfiguration
at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean.(AnnotationSessionFactoryBean.java:65)

The specific jar-files needed to be included under /web-inf/lib directory by using lib specific ant task.

This resolved the packinging problem. The installation of MySQL server was highlighted in Red Hat Enterprise Linux specific installation instructions. Also some ports were necessary to open when using iptables…the Iptables tutorial gives good ways how to specify which port should be opened. The reading of application specific configuration file was done using instrouction posted on Sun’s java forum

Add comment June 26th, 2007

Linux security news and other Spring Developer kit from Oracle

Hi,

The need for assessment of different Linux distributions have raised questions in the back of my mind…hmm after reading the MiMi Yeh article about Platform and application security: Linux distributions,AppArmor and more made me think of the differences of security features between Solaris and other Linux flavors. Other very well written article about Bastille-Linux - Introduction and Installation also is worth reading. The list of Linux Redhat Linux and Suse security how-tos are listed in summary of Enterprise Linux site. RedHat Linux firewall configuration and the security triangle tripwire -nessus-Snort is also discussed in this article. The sniffing of packages in the network between your machine and the wan-network can be carried out by using such application as Wireshark or Etherreal
Oracle unveiled it’s Dev Kit for Spring...need to take a evaluate ride with the usage of these tools…more about these issues later.

Add comment May 24th, 2007

The clustering aspects of Spring using Terracotta….now available for free

Hi,

The need of clustering has been a nice function mainly provided by application servers and clustering has been locked to some proprietary solution of JEE application server. This doesn’t have to be the case any more as Terracotta is providing Open source clustering for Java

and Terracotta officially provides clustering for Spring - see use case listings

In addition, In javaone 2006 Trasparently clustering Spring - Runtime solution for Java
Technology

and Terracotta highlights the Spring sample application clustering of Spring Beans in the turorial

TutorialTerracottaForSessionsJPetstore

Terracotta For Spring Clustering Tutorial 



Happy Clustering

Add comment April 4th, 2007

Oracle Spring breeze….lerning linux commands

Hi,

The past week has been learning experience how to install yet good old RDMD system in Feodora Linux 6. In evaluating these open source Databases I have come to conclusion to have three DB vendors a try…these are small foot-print and efficient DB called Berkley DB , Oracle owned Open source DB , acquired by Oracle last year,which was formely known as Sleepycat software company (alternative to IBM sponsored Apache Derby DB formely known as Cloudscape). As you might quess already Apache Derby deserves a shot. Third runner-up is Postgres DB

The Spring Jpetstore sample application was installed in a breeze by using Oracle JDeveloper 10g IDE. Installation instruction how to install this bundle to your linux system can be found on article how to install Oracle JDeveloper 10g on Linux . Useful links to Linux commands mastery is provided also in Oracle sites Guide to Linux Commands mastery

and pay also visit to site Guide to Advanced Linux Commands mastery

and finally the Spring section provided by Oracle - Getting Started with Spring 2.0 shows hot to build the Spring framework sample application JPetstore. The only modifications which I needed to do was to ship the JDBC drivers for each DB and change ../WEB-INF/jdbc.properties file and try the sample application and I was impressed by it simplicity.

One good handy way to configure .bash_profie file when creating environment variables or aliases is to source the file…meaning it won’t be necessary to reloging/reboot the os in order to the .bash_profile modification to be valid.

cd
. ./.bash_profile

		

Add comment April 2nd, 2007

Using Maven 2 to build Bea Portal Applications…and Spring intro

Hi,

Bea provided a great Technical article Using Maven 2 to Build Weblogic Portal Applicaitons
…it provides conviniently step by steep instructions to proceed with the creation of skeleton code base for Bea Portal projects. In addition, as BEA is officially supporting Spring framework as supported framework it it also convinient to become familiar with Spring - you may start by exploring the introductive article into Spring usage -

Better J2eeing with Spring.

The good old DAO specific Data Access Object Design Pattern could be in springfied application to be replaced wit Spring’s Data Access Object Framework as a sample is provided in BEA’s article - A Priimer on Spring’s Data Access Object (DAO) Framework.
.

This article introduces how to configure datasourece in Weblogic Server 9.0 - Configuring Weblogic Server 9.x JDBC . The peformance of Weblogic Server Portal is highlighted in this article - Peformance Management 101 for Weblogic Portal .

Other useful Spring specific training material can be found in BeJug-site

Add comment March 12th, 2007

Bea Weblogic support for Spring…Spring is here

In my quest to become aquainted with the Spring framework I became aware of how steadily Bea has supported Spring framework - as one can conclude oneself by visiting Bea’s Spring Resource site.Good article to read about Bea’s commitment is article Spring 1.2.x Integration With Weblogic Server. The article focuses on explaining how the Spring enchandes the software development by providing the developer tools to concentrate on core business logic development - no more tedious application server specific plumming. As a sample application Avitek Medical Records ( also know as MedRec) is provided to highlight the technical nices how the integration was practiacally carried out.

I’m currenlty also reading Manning’s publication Action series - Spring in Action

Also very compact and core issues of Spring are also covered in detail - in developer-friendly way in Spring - A developer’s Notebook….indeed good tutorial for spring newbies and also good reference material for skilled developers utilizing Spring framework.

Add comment March 2nd, 2007