Cześć, robię aplikacje w stylu CRUD, na razie staneło na "C" mam klasy wydaje mi się że połączenia też są ok ale jednak Http 500.
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Student
{
@Id
@GeneratedValue ( strategy = GenerationType.AUTO)
@Column
private int id;
@Column
private String imie;
public Student ()
{
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImie() {
return imie;
}
public void setImie(String imie) {
this.imie = imie;
}
}
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.mkyong.rest.HibernateUtil;
public class StudentDao
{
public void addUser(Student student) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
session.save(student);
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
}
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
@Path("/message")
public class MessageRestService
{
StudentDao dao = new StudentDao();
@GET
@Path("/{param}")
public Response printMessage(@PathParam("param") String msg)
{
String result = "Restful example : " + msg;
Student s = new Student();
s.setImie(msg);
dao.addUser(s);
return Response.status(200).entity(result).build();
}
}
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Connection settings -->
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Student</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">admin</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Print executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create all database on startup -->
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<!-- Annotated entity classes -->
<mapping class="com.mkyong.app.Student"/>
</session-factory>
</hibernate-configuration>