Skip to content
Snippets Groups Projects
Commit c5bdb7d9 authored by Goik Martin's avatar Goik Martin
Browse files

Transient attribs

parent 0eea7cdb
No related branches found
No related tags found
No related merge requests found
package hibintro.v4;
import hibintro.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
* @author goik
*
* Persisting a simple instance of {@link User}
*
*/
public class PersistSingleUser {
/**
* @param args not used.
*/
public static void main(String[] args) {
final Session session = HibernateUtil.createSessionFactory("hibintro/v4/hibernate.cfg.xml").openSession();
final Transaction transaction = session.beginTransaction();
{
final User u = new User("goik", null);
session.save(u);
}
transaction.commit();
}
}
\ No newline at end of file
package hibintro.v4;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* @author goik
*
* A simple class indended to be mapped to a database server
*
*/
@Entity
public class User {
String uid;
/**
*
* @return The user's unique login name e.g. "goik"
*/
@Id
public String getUid() {
return uid;
}
/**
* @param uid See {@link #getUid()}.
*/
public void setUid(String uid) {
this.uid = uid;
}
String cname;
/**
* @return The user's common name e.g. "Martin Goik"
*/
@Column(nullable = false)
public String getCname() {
return cname;
}
/**
* @param cname See {@link #getCname()}.
*/
public void setCname(String cname) {
this.cname = cname;
}
/**
* Hibernate/JPA require a default constructor
*/
public User() {}
/**
* @param uid See {@link #getUid()}.
* @param cname See {@link #getCname()}.
*/
public User(String uid, String cname) {
this.uid = uid;
this.cname = cname;
}
}
\ No newline at end of file
<?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.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">XYZ</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hdm</property>
<property name="hibernate.connection.username">hdmuser</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="hibintro.v4.User"/>
</session-factory>
</hibernate-configuration>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment