Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testehibernate;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
- import org.hibernate.cfg.Configuration;
- public class TesteHibernate
- {
- public static void main(String[] args)
- {
- Configuration configuration = new Configuration().configure();
- StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
- SessionFactory factory = configuration.buildSessionFactory(builder.build());
- Session session = factory.openSession();
- Teste teste = new Teste();
- teste.setNome("Alexandre");
- teste.setEmail("alexandre@email.com");
- session.beginTransaction();
- session.save(teste);
- session.getTransaction().commit();
- session.close();
- }
- }
- package testehibernate;
- import javax.persistence.Entity;
- import javax.persistence.Id;
- @Entity
- public class Teste
- {
- @Id
- private int id;
- private String nome;
- private String email;
- public int getId()
- {
- return id;
- }
- public void setId(int id)
- {
- this.id = id;
- }
- public String getNome()
- {
- return nome;
- }
- public void setNome(String nome)
- {
- this.nome = nome;
- }
- public String getEmail()
- {
- return email;
- }
- public void setEmail(String email)
- {
- this.email = email;
- }
- }
- CREATE TABLE `teste` (
- `id` int(11) NOT NULL,
- `nome` varchar(255) NOT NULL,
- `email` varchar(255) NOT NULL
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
- ALTER TABLE `teste`
- ADD PRIMARY KEY (`id`);
- TO_INCREMENT for table `teste`
- ALTER TABLE `teste`
- MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=14;
- <?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>
- <!-- Database connection settings -->
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://localhost:3306/testdb</property>
- <property name="connection.username">root</property>
- <property name="connection.password"></property>
- <!-- JDBC connection pool (use the built-in) -->
- <property name="connection.pool_size">1</property>
- <!-- SQL dialect -->
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <!-- Echo all executed SQL to stdout -->
- <property name="show_sql">true</property>
- <!-- Mapping files -->
- <mapping class="testehibernate.Teste"/>
- </session-factory>
- </hibernate-configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement