This is the userDo
Code:
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name = "USER", uniqueConstraints = { @UniqueConstraint(columnNames = { "user_id" }) })
public class UserDO {
@Id
@Column(name = "user_id", columnDefinition = "VARCHAR(255) NOT NULL DEFAULT ''", insertable = true)
private String userId;
@Column(name = "password", columnDefinition = "VARCHAR(255) NOT NULL DEFAULT ''", insertable = true)
private String password;
@Column(name = "first_name", columnDefinition = "VARCHAR(255) NOT NULL DEFAULT ''", insertable = true)
private String firstName;
@Column(name = "last_name", columnDefinition = "VARCHAR(255) NOT NULL DEFAULT ''", insertable = true)
private String lastName;
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", nullable = false)
@org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<AddressDO> userAddress;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Set<AddressDO> getUserAdrress() {
return this.userAddress;
}
public void setUserAdrress(Set<AddressDO> userAddress) {
this.userAddress = userAddress;
}
}
this is AddressDO
Code:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name = "ADDRESS",uniqueConstraints = {
@UniqueConstraint(columnNames={"address_id"})}
)
public class AddressDO {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "address_id", insertable = true)
private long address_id;
@Column(name = "city", columnDefinition = "VARCHAR(100) NOT NULL DEFAULT ''", insertable = true)
private String city;
@Column(name = "state", columnDefinition = "VARCHAR(100) NOT NULL DEFAULT ''", insertable = true)
private String state;
@Column(name = "zipcode", columnDefinition = "VARCHAR(25) NOT NULL DEFAULT ''", insertable = true)
private String zipcode;
public AddressDO() {
}
public AddressDO(String state, String street,
String zipcode) {
this.state = state;
this.street = street;
this.zipcode = zipcode;
}
public long getAddress_id() {
return address_id;
}
public void setAddress_id(long address_id) {
this.address_id = address_id;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
then my functioning class
Code:
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import com.cognizant.cloudone.dal.dao.intf.UserDAO;
import com.cognizant.cloudone.dal.dbo.UserDO;
public class UserDaoImpl extends BaseDaoImpl implements
UserDAO {
public UserDO create(UserDO userDo) {
return excute(userDo, DataBaseOperationType.create);
}
public int delete(UserDO userDo) {
excute(userDo, DataBaseOperationType.delete);
return 1;
}
public UserDO update(UserDO userDo) {
return excute(userDo, DataBaseOperationType.update);
}
private UserDO excute(UserDO userDo, DataBaseOperationType operation) {
Transaction tx = null;
Session session = getSession();
try {
tx = session.beginTransaction();
switch (operation) {
case create:
session.save(userDo);
break;
case update:
session.update(userDo);
break;
case delete:
session.delete(userDo);
break;
default:
break;
}
tx.commit();
return userDo;
} catch (RuntimeException e) {
if (tx != null && tx.isActive()) {
try {
tx.rollback();
} catch (HibernateException e1) {
logger.debug("Error rolling back transaction");
}
}
}
return null;
}
}