Difference between revisions of "Hibernate"

(POJO class)
Line 80: Line 80:
 
package com.oasissofttech.springmvc;
 
package com.oasissofttech.springmvc;
  
 +
import javax.persistence.Column;
 
import javax.persistence.Entity;
 
import javax.persistence.Entity;
 +
import javax.persistence.GeneratedValue;
 +
import javax.persistence.GenerationType;
 
import javax.persistence.Id;
 
import javax.persistence.Id;
 +
import javax.persistence.Table;
 +
import javax.persistence.Transient;
  
 
@Entity
 
@Entity
public class Alien {
+
@Table(name = "metric")
 +
public class ParameterTable {
 
@Id
 
@Id
private int aid;
+
@GeneratedValue(strategy = GenerationType.AUTO)
private String aname;
+
@Column(name = "PRM_ID")
private String acolor;
+
private int pid;
 +
 +
@Column(name = "PRM_NAME")
 +
private String parameterName;
  
public Alien() {
+
@Column(name = "PRM_DESC")
 +
private String parameterDesc;
 +
 
 +
//transient for temporary data
 +
@Transient
 +
private String doNotCreateThisColumnInTable;
 +
 
 +
public ParameterTable() {
 
}
 
}
  
public Alien(int aid, String aname, String acolor) {
+
public ParameterTable(int pid, String parameterName, String parameterDesc) {
 
super();
 
super();
this.aid = aid;
+
this.pid = pid;
this.aname = aname;
+
this.parameterName = parameterName;
this.acolor = acolor;
+
this.parameterDesc = parameterDesc;
 +
}
 +
 
 +
public int getPid() {
 +
return pid;
 
}
 
}
  
public int getAid() {
+
public void setPid(int pid) {
return aid;
+
this.pid = pid;
 
}
 
}
  
public void setAid(int aid) {
+
public String getParameterName() {
this.aid = aid;
+
return parameterName;
 
}
 
}
  
public String getAname() {
+
public void setParameterName(String parameterName) {
return aname;
+
this.parameterName = parameterName;
 
}
 
}
  
public void setAname(String aname) {
+
public String getParameterDesc() {
this.aname = aname;
+
return parameterDesc;
 
}
 
}
  
public String getAcolor() {
+
public void setParameterDesc(String parameterDesc) {
return acolor;
+
this.parameterDesc = parameterDesc;
 
}
 
}
  
public void setAcolor(String acolor) {
+
public String getDoNotCreateThisColumnInTable() {
this.acolor = acolor;
+
return doNotCreateThisColumnInTable;
 +
}
 +
 
 +
public void setDoNotCreateThisColumnInTable(String doNotCreateThisColumnInTable) {
 +
this.doNotCreateThisColumnInTable = doNotCreateThisColumnInTable;
 
}
 
}
  
 
}
 
}
 +
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 13:20, 21 June 2018

Springframework - Hibernate - MySQL - Maven Project

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.oasissofttech.springmvc</groupId>
	<artifactId>SpringFramework-MVC-Tutorial-4-Hibernate</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringFramework-MVC-Tutorial-4-Hibernate Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>

		<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>5.3.1.Final</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.0.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.0.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.39</version>
		</dependency>


	</dependencies>
	<build>
		<finalName>SpringFramework-MVC-Tutorial-4-Hibernate</finalName>
	</build>
</project>

hibernate.cfg.xml

<?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>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://testmysql.oasissofttech.com:3306/oasis_test_db</property>
        <property name="hibernate.connection.username">oasisdbtestuser</property>
        <property name="hibernate.connection.password">test97531</property>
        <!--  <property name="hbm2ddl.auto">create</property>  - deletes table and create new -->
        <!--  <property name="hbm2ddl.auto">update</property>  - if exist run the query, otherwise create table first-->
        <!--  <property name="show_sql">true</property>  - shows all queries created by hibernate-->
    </session-factory>
</hibernate-configuration>

POJO class

package com.oasissofttech.springmvc;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "metric")
public class ParameterTable {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "PRM_ID")
	private int pid;
	
	@Column(name = "PRM_NAME")
	private String parameterName;

	@Column(name = "PRM_DESC")
	private String parameterDesc;

	//transient for temporary data
	@Transient
	private String doNotCreateThisColumnInTable;

	public ParameterTable() {
	}

	public ParameterTable(int pid, String parameterName, String parameterDesc) {
		super();
		this.pid = pid;
		this.parameterName = parameterName;
		this.parameterDesc = parameterDesc;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public String getParameterName() {
		return parameterName;
	}

	public void setParameterName(String parameterName) {
		this.parameterName = parameterName;
	}

	public String getParameterDesc() {
		return parameterDesc;
	}

	public void setParameterDesc(String parameterDesc) {
		this.parameterDesc = parameterDesc;
	}

	public String getDoNotCreateThisColumnInTable() {
		return doNotCreateThisColumnInTable;
	}

	public void setDoNotCreateThisColumnInTable(String doNotCreateThisColumnInTable) {
		this.doNotCreateThisColumnInTable = doNotCreateThisColumnInTable;
	}

}

Application main class

package com.oasissofttech.springmvc;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class App {

	public static void main(String[] args) {
		Alien alien = new Alien();
		alien.setAid(1);
		alien.setAname("rasim");
		alien.setAcolor("green");

		Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class);
		SessionFactory buildSessionFactory = con.buildSessionFactory();
		Session session = buildSessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		session.save(alien);
		transaction.commit();

	}

}



JBOSS - Hibernate Tools

JBoss Tools eClipse plugin is very powerfull tool to manage your hibernate objects and queries.

Hibernate Tools provides easy generation, testing and prototyping of your Hibernate or JPA mapped projects. Use it to Run queries, browse mappings and generate code for your data projects.

Hibernate Code Generation Launch

Generate artifacts based on Hibernate model

Hibernate Tools provide a code generation launch configuration which can take the configured Hibernate model and use for various code generation tasks.

Since it is a launch configuration it is easy to run it repeatedly while developing via the Launch Configuration menu.

features-hibernate-launchconfig-reveng.png


Reverse Engineering

Exporters

Hibernate Console

Browse your model & database

Mapping Diagram

Mapping Diagram controls

HQL Editor

Prototype your queries In the HQL editor you can prototype your HQL queries and see the result in the Hibernate Query Result viewer.

Dynamic SQL Preview

features-hibernate-dynamic-sql-preview.png

Criteria Editor

hbm.xml XML Editor

features-hibernate-hbmxml-editor.png

Reverse Engineering Strategy Editor

Hibernate eclipse plugin - JBOSS : https://tools.jboss.org/features/hibernate.html