/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
*
* @author Admin
*/
@Entity
@Table(name = "users")
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findById", query = "SELECT u FROM Users u WHERE u.id = :id"),
@NamedQuery(name = "Users.findByName", query = "SELECT u FROM Users u WHERE u.name = :name"),
@NamedQuery(name = "Users.findBySurname", query = "SELECT u FROM Users u WHERE u.surname = :surname")})
public class Users implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "id")
private BigDecimal id;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
public Users() {
}
public Users(BigDecimal id) {
this.id = id;
}
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Users)) {
return false;
}
Users other = (Users) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.Users[id=" + id + "]";
}
}
использование DAO, service и пр
Код:
usersDAO
Код:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
*
* @author Admin
*/
import com.Users;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class usersDAO extends HibernateDaoSupport implements IusersDAO {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyAppPU");
HibernateTemplate hibernateTemplate;
public void save(Users user) {
getHibernateTemplate().saveOrUpdate(user);
}
public void delete(Users user) {
getHibernateTemplate().saveOrUpdate(user);
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
*
* @author Admin
*/
import com.Users;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class usersDAO extends HibernateDaoSupport implements IusersDAO {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyAppPU");
HibernateTemplate hibernateTemplate;
public void save(Users user) {
getHibernateTemplate().saveOrUpdate(user);
}
public void delete(Users user) {
getHibernateTemplate().saveOrUpdate(user);
}
}
IusersService
Код:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package service;
/**
*
* @author Admin
*/
import com.Users;
import dao.IusersDAO;
public class usersService implements IusersService{
private IusersDAO dao;
public void deleteUser(Users user){
dao.save(user);
}
public void saveUser(Users user){
dao.delete(user);
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package service;
/**
*
* @author Admin
*/
import com.Users;
import dao.IusersDAO;
public class usersService implements IusersService{
private IusersDAO dao;
public void deleteUser(Users user){
dao.save(user);
}
public void saveUser(Users user){
dao.delete(user);
}
}
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>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">root</property>
</session-factory>
</hibernate-configuration>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">root</property>
</session-factory>
</hibernate-configuration>
run.java
Код:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com;
/**
*
* @author Admin
*/
import java.math.BigDecimal;
import service.IusersService;
public class run {
private static IusersService service;
public static void main(String args[]){
Users usr = new Users();
usr.setId(BigDecimal.ONE);
usr.setName("jake");
usr.setSurname("smirnov");
service.saveUser(usr);
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com;
/**
*
* @author Admin
*/
import java.math.BigDecimal;
import service.IusersService;
public class run {
private static IusersService service;
public static void main(String args[]){
Users usr = new Users();
usr.setId(BigDecimal.ONE);
usr.setName("jake");
usr.setSurname("smirnov");
service.saveUser(usr);
}
}
вылетает на строчке service.saveUser(usr);