[BAEL-4635] Tests for JPA Transient Annotation tutorial
This commit is contained in:
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.ignorable.fields;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
public class HibernateConfig {
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
public static SessionFactory getSessionFactory() {
|
||||
if (sessionFactory == null) {
|
||||
try {
|
||||
Configuration configuration = new Configuration();
|
||||
|
||||
Properties settings = new Properties();
|
||||
settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
|
||||
settings.put(Environment.URL, "jdbc:mysql://localhost:3306/app_db?useSSL=false");
|
||||
settings.put(Environment.USER, "root");
|
||||
settings.put(Environment.PASS, "password");
|
||||
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
|
||||
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
|
||||
configuration.setProperties(settings);
|
||||
|
||||
configuration.addAnnotatedClass(User.class);
|
||||
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
|
||||
.build();
|
||||
|
||||
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return sessionFactory;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.ignorable.fields;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Users")
|
||||
public class User implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer id;
|
||||
private String email;
|
||||
private String password;
|
||||
@Transient
|
||||
private String currentDevice;
|
||||
|
||||
// Needed for Hibernate mapping
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String email, String password, String currentDevice) {
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
this.currentDevice = currentDevice;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getCurrentDevice() {
|
||||
return currentDevice;
|
||||
}
|
||||
|
||||
public void setCurrentDevice(String currentDevice) {
|
||||
this.currentDevice = currentDevice;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", User.class.getSimpleName() + "[", "]").add("id=" + id)
|
||||
.add("email='" + email + "'")
|
||||
.add("password='" + password + "'")
|
||||
.add("currentDevice='" + currentDevice + "'")
|
||||
.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (!(o instanceof User))
|
||||
return false;
|
||||
User user = (User) o;
|
||||
return email.equals(user.email);
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.ignorable.fields;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
|
||||
public class UserDao {
|
||||
|
||||
public void saveUser(User user) {
|
||||
Transaction transaction = null;
|
||||
try (Session session = HibernateConfig.getSessionFactory()
|
||||
.openSession()) {
|
||||
transaction = session.beginTransaction();
|
||||
session.save(user);
|
||||
transaction.commit();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
if (transaction != null) {
|
||||
transaction.rollback();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<User> getUsers() {
|
||||
try (Session session = HibernateConfig.getSessionFactory()
|
||||
.openSession()) {
|
||||
return session.createQuery("from User", User.class)
|
||||
.list();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user