Tuesday, July 21, 2015

Hibernate 4 one to one mapping with MySql

This is a basic working example of OneToOne Mapping with Hibernate 4 and MySQL.First you have to create a database named test on MySQL and start the thing. Then give your MySQL root user name and password to ,

 <property name="hibernate.connection.username">root</property> //user Name
    <property name="hibernate.connection.password">root</property> //password
in  hibernate.cfg.xml file.

And then prepare following files.

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://localhost:3306/test</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    <mapping class="com.dilan.model.User" />
    <mapping class="com.dilan.oneToOne.Customer" />
    <mapping class="com.dilan.oneToOne.CustomerDeatail" />
</session-factory>
</hibernate-configuration>

Customer.java

package com.dilan.oneToOne;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="Customer")
public class Customer {
@Id
@GeneratedValue
@Column(name="CUSTOMER_ID")
private int customerId;
@Column(name="FIRST_NAME")
private String customerFisrName;
@Column(name="LAST_NAME")
private String customerLastName;
@OneToOne(mappedBy="customer", cascade=CascadeType.ALL)
private CustomerDeatail customerDeatail;
public Customer(){}
public Customer(String customerFisrName,
String customerLastName, CustomerDeatail customerDeatail) {
super();
this.customerFisrName = customerFisrName;
this.customerLastName = customerLastName;
this.customerDeatail = customerDeatail;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerFisrName() {
return customerFisrName;
}
public void setCustomerFisrName(String customerFisrName) {
this.customerFisrName = customerFisrName;
}
public String getCustomerLastName() {
return customerLastName;
}
public void setCustomerLastName(String customerLastName) {
this.customerLastName = customerLastName;
}
public CustomerDeatail getCustomerDeatail() {
return customerDeatail;
}
public void setCustomerDeatail(CustomerDeatail customerDeatail) {
this.customerDeatail = customerDeatail;
}

}

CustomerDeatail.java

package com.dilan.oneToOne;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
@Entity
@Table(name="CUSTOMER_DETAIL")
public class CustomerDeatail {
@GenericGenerator(name="gen", strategy = "foreign",
parameters = @Parameter(name = "property", value = "customer"))
@Id
@Column(name="CUSTOMER_ID")
@GeneratedValue(generator="gen")
private int customerId;
@Column(name="HEIGHT")
private int height;
@Column(name="WEIGHT")
private int weight;
@OneToOne
@PrimaryKeyJoinColumn
private Customer customer;
public CustomerDeatail(){}
public CustomerDeatail( int height, int weight,
Customer customer) {
super();
this.height = height;
this.weight = weight;
this.customer = customer;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}

}

App.java

package com.dilan.app;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import com.dilan.model.User;
import com.dilan.oneToOne.Customer;
import com.dilan.oneToOne.CustomerDeatail;

public class App {

public static void main(String[] args) {
     

        Configuration configuration = new Configuration().configure();

        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
       
        Session session = sessionFactory.openSession();
       
        session.beginTransaction();
           
     
        CustomerDeatail customerDeatail = new CustomerDeatail();
        customerDeatail.setHeight(163);
        customerDeatail.setWeight(75);
        Customer customer = new Customer("Dilan","priyankara",customerDeatail);
        customerDeatail.setCustomer(customer);
       
        session.save(customer);
        session.getTransaction().commit();
       
        System.out.println("Done");

}

}

Run the App.java file and it will create the tables and insert data as OneToOne mapping.




Tuesday, December 2, 2014

get percentages on SQL querry - Oracle

If we want to get the percentage of some column on same query, we can do it using "ratio_to_report() over()" function.
ratio_to_report(Number_of_books) over()
let's say we have following table called tbl_books.

Book Number_of_books
Book1 30
Book1 20
Book1 10

if we want to get percentages of each book over total number of books we can do as following,
select book,ratio_to_report(Number_of_books) over() * 100 as percentage from tbl_books
it gives,

Book percentage
Book1 50
Book1 33.333333333
Book1 16.66666666


Tuesday, October 7, 2014

Where does MySQL store data in PC


Windows

Find the my.ini file which store in the MySQL installation folder.
and then open the file and Find the “datadir”, this is the where does MySQL stored the data in Windows.
datadir="C:/ProgramData/MySQL/MySQL Server 5.5/Data/"

Linux

Find the my.cnf with the sudo find / -name my.cnf command.
2) viewthe my.cnf file cat /etc/mysql/my.cnf
yongmo@myserver:~$ cat /etc/mysql/my.cnf
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
[mysqld]
#
# * Basic Settings
#
user   = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket  = /var/run/mysqld/mysqld.sock
port   = 3306
basedir  = /usr
datadir  = /var/lib/mysql
tmpdir  = /tmp
language = /usr/share/mysql/english
skip-external-locking

3) Find the “datadir”, this is where does MySQL stored the data in Linux system. 

Saturday, January 19, 2013

Install a .sql file to a database in MySQL

First open the command prompt and go to the your MySQL installed directory and go to the bin folder in command prompt.Then copy your database file(.sql file) to that directory(C:\Program Files\MySQL\MySQL Server 5.5\bin).
Then in command prompt run this code.

C:\Program Files\MySQL\MySQL Server 5.5\bin>mysql --user=USER_NAME --password=PASSWORD DATABASE_NAME<FILE_NAME.sql


As a example let's think my MySQL user name is root and password is dilan and I want to install a file called queries.sql to the database named myTestDB. The code snippet  will be as following.

C:\Program Files\MySQL\MySQL Server 5.5\bin>mysql --user=root --password=dilan myTestDB<queries.sql

Saturday, December 15, 2012

Install maven in windows 7


Install maven in windows 7.

Actually Apache Maven is not need to install on windows.It can be downloaded as a .zip file and extract it as a local directory and configure it to windows.

  • To this task java development kit(JDK) should be installed in your windows PC.First check JDK is installed and JAVA_HOME variable is set in the windows environmental variables.



          Right-click on my computer and select properties.
         Then select Advance system settings.
              After that select  Environmental variables button from System properties window.

              Then make sure your JAVA_HOME variable set to the JDK folder.If not set it to the JDK folder.


  • Then download Apache Maven and extract it.

                    Download Apache Maven from the http://maven.apache.org/download.html. Select a version and click on it. Then Extract it to some where in your PC. I have renamed the folder as maven.


  • Add MAVEN_HOME 

    Add MAVEN_HOME to the environmental variables,set it to your maven root folder. 



  • Update PATH

   update your PATH variable in the environmental variables with maven bin folder. After this you can use maven command any where in your file system. 

then click OK on all the widows and you are done...:-)


  • Check

    To check you install maven successfully type mvn -version on your command line.
If you see this kind of message congratulation you have successfully installed maven... :-)