Hibernate
Hibernate
Features:
Open-Source Lightweight
Fast performance [cause of internal cache memory].
DB Independent Query [HQL query features]-> it
automatically creates the queries for us, we don’t need to
write it manually.
Automatic Table Creation.
Simplifies Complex Join. -> @JoinColumn, @OneToMany,
@ManyToOne, Fetch = FetchType.Lazy etc.
“Hibernate Architecture”
It provides many objects such as persistent object, session
factory, transaction factory, connection factory, session,
transaction etc.
1. Java Application Layer
2. Hibernate Framework Layer
3. Backend Api Layer
4. Database Layer
2. Session:
The session object provides an interface between
application and data stored in the database.
It has internal JDBC connection. It provides factory
methods for Transaction, Query and Criteria.
The org.hibernate.Session interface provides methods
to insert, update and delete the object.
3. Transaction:
It is used for transaction management in the program.
It provides the atomic unit of work. [One by one].
The org.hibernate.Transaction provides methods to
manage the transaction.
4. ConnectionProvider:
It is a factory of JDBC Connection and it has an abstract
property for Driver Manager and Data Source.
5. TransactionalFactory:
It is a factory of Transaction and it is a optional part.
# Hibernate properties
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.diale
ct.MySQLDialect
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
public class HibernateConfig {
@Bean
public LocalSessionFactoryBean sessionFactory(DataSource
dataSource) {
LocalSessionFactoryBean sessionFactoryBean = new
LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setPackagesToScan("com.acejobber.chatserver.E
ntity"); // Specify packages to scan for entity classes
hibernateProperties.setProperty("hibernate.globally_quoted_identifie
rs", "true");
hibernateProperties.setProperty("hibernate.connection.provider_clas
s", "org.hibernate.connection.C3P0ConnectionProvider");
hibernateProperties.setProperty("hibernate.c3p0.min_size",
"5");
hibernateProperties.setProperty("hibernate.c3p0.max_size",
"20");
hibernateProperties.setProperty("hibernate.c3p0.timeout",
"1800");
hibernateProperties.setProperty("hibernate.c3p0.max_statements",
"50");
hibernateProperties.setProperty("hibernate.c3p0.idle_test_period",
"3000");
hibernateProperties.setProperty("hibernate.c3p0.test_connection_on
_checkout", "true");
hibernateProperties.setProperty("hibernate.c3p0.preferredTestQuery
", "SELECT 1;");
hibernateProperties.setProperty("hibernate.c3p0.debugUnreturnedC
onnectionStackTraces", "true");
sessionFactoryBean.setHibernateProperties(hibernateProperties);
return sessionFactoryBean;
}
}
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto= update
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql: true
# debug=true
spring.main.allow-circular-references=true
logging.level.org.springframework=DEBUG
logging.level.org.hibernate=DEBUG
<hibernate-configuration>
<session-factory>
<!-- Database Dialect -->
<property
name="hibernate.dialect">org.hibernate.dialect.PostgreS
QLDialect</property>
</session-factory>
</hibernate-configuration>
import com.acejobber.chatserver.Entity.Conversation;
import com.acejobber.chatserver.Entity.Message;
import com.acejobber.chatserver.Entity.MessageInbox;
import com.acejobber.chatserver.MainService.ChatEntity;
@SpringBootApplication
public class ChatserverApplication {
SpringApplication.run(ChatserverApplication.class, args);
@Bean
public SessionFactory getSession() {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml").addAnnotatedClass(Messa
ge.class);
configuration.configure("hibernate.cfg.xml").addAnnotatedClass(Messa
geInbox.class);
configuration.configure("hibernate.cfg.xml").addAnnotatedClass(Conve
rsation.class);
configuration.configure("hibernate.cfg.xml").addAnnotatedClass(ChatE
ntity.class);
SessionFactory sessionFactory =
configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
System.out.println(session.isConnected());
// return session;
return sessionFactory;
}
}
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Data
@Table(name = "inbox", schema = "sdtest")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class MessageInbox {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "inbox_id")
private Long id;
@Column
private Long receiverId; // chatter ID
@Column
private Long senderId; // User ID
@JsonManagedReference
@OneToMany(mappedBy = "inbox", fetch = FetchType.EAGER, cascade
= CascadeType.ALL)
private List<Message> messages;
@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(name = "conversation_id")
private Conversation conversation;