Thursday, March 26, 2009

How Long Does It Take For Bichon Hair To Grow

Hibernate - Hibernate

first problem with the mismatch of the world of object-oriented and relational occurs when modeling (mapping) a hierarchy of classes. Inheritance is one of the paradigms of object-oriented programming and the abandonment of him, because of the relational data layer, it would be regression. Using Hibernate inheritance can be mapped in three ways: first
table for each class (table per concrete class)
second table for each class hierarchy (table per class hierarchy)
third table for each subclass (table per subclass)

In all cases, I will perform the method:

 
public class Main {public static
void main (String [] args) {Session session
= null;
SessionFactory = new SessionFactory Configuration (.) configure (). buildSessionFactory ();
session = sessionFactory.openSession();
Subcategory1 cat = new Subcategory1();
cat.setName("sub1");
cat.setSize(1);
Subcategory2 cat2 = new Subcategory2();
cat2.setName("sub2");
cat2.setVolume(2);
session.beginTransaction();
session.save(cat);
session.save(cat2);
session.flush();
session.getTransaction().commit();
session.close();
session = sessionFactory.openSession();
session.beginTransaction();
List<Category> cats = session.createQuery("from Subcategory1").list();
System.out.println(cats.size());
cats = session.createQuery("from Subcategory2").list();
System.out.println(cats.size());
}
}


public class Category {
Integer id;
String name;
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

public class Subcategory1 extends Category {
Integer id;
Integer size;
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Integer getSize() {
return size;
}

public void setSize(Integer size) {
this.size = size;
}
}

public class Subcategory2 extends Category {
Integer id;
Integer volume;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
} Public Integer

getVolume () {return
volume;
} public void

setVolume (Integer volume) {
this.volume = volume;

}}


Ad 1
simplest approach - the database in general are not aware of the inheritance.

consists of two files mapping Subcategory1.hbm.xml, Subcategory2.hbm.xml:

 
\u0026lt;? Xml version = "1.0"?>

\u0026lt;! DOCTYPE hibernate-mapping PUBLIC
"- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN "
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

<hibernate-mapping package="main">
<class name="Subcategory1">
<id name="id" unsaved-value="null" column="category_id">
<generator class="sequence">
<param name="sequence">sub1_category_seq</param>
</generator>
</id>
<property name="name"/>
<property name="size"/>
</class>
</hibernate-mapping>


<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="main">
<class name="Subcategory2">
<id name="id" unsaved-value="null" column="category_id">
<generator class="sequence">
<param name="sequence">sub2_category_seq</param>
</generator>
</id>
<property name="name"/>
<property name="volume"/>
</class>
</hibernate-mapping>


Stworzone zostaly dwie tabele:

  
CREATE TABLE subcategory1
(
category_id integer NOT NULL,
name character varying(255),
size integer,
CONSTRAINT subcategory1_pkey PRIMARY KEY (category_id)
)

CREATE TABLE subcategory2
(
category_id integer NOT NULL,
name character varying(255),
volume integer,
Subcategory2_pkey CONSTRAINT PRIMARY KEY (category_id)
)


Ad2.
second approach - mapping of the entire hierarchy of classes in a single table.
form a single file mapping Category.hbm.xml

 
\u0026lt;? Xml version = "1.0"?>

\u0026lt;! DOCTYPE hibernate-mapping PUBLIC
"- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN "
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

\u0026lt;hibernate-mapping package="main">
\u0026lt;class name="Category">
\u0026lt;id name = "id" unsaved-value = "null" column="category_id">
<generator class="sequence">
<param name="sequence">category_seq</param>
</generator>
</id>
<discriminator column="type" type="string" />
<property name="name" />
<subclass name="Subcategory1" discriminator-value="1">
<property name="size" />
</subclass>
<subclass name="Subcategory2" discriminator-value="2">
\u0026lt;property name="volume" />
\u0026lt;/ subclass>
\u0026lt;/ class>
\u0026lt;/ hibernate-mapping>


was created a table:

 

CREATE TABLE category (
category_id integer NOT NULL,
"type" character varying (255) NOT NULL, name character varying
(255),
size integer, integer
volume,
category_pkey CONSTRAINT PRIMARY KEY (category_id)
)

Important! When creating the mapping file, be sure to element \u0026lt;discriminator> was in before all the elements \u0026lt;PROPERTY>.

Ad 3
third approach involves the representation of inheritance relationships with foreign keys of relational links. Each subclass uses its own table. I create three files
mapping Category.hbm.xml, Subcategory1.hbm.xml, Subcategory2.hbm.xml

 
\u0026lt;? Xml version = "1.0"?>

\u0026lt;! DOCTYPE hibernate-mapping PUBLIC
"- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

\u0026lt;hibernate-mapping package="main">
<class name="Category">
<id name="id" unsaved-value="null" column="category_id">
<generator class="sequence">
<param name="sequence">category_seq</param>
</generator>
</id>
<property name="name" />

</class>
</hibernate-mapping>


<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="main">
<joined-subclass name="Subcategory1" extends="Category">
<key column="category_id"/>
<property name="size"/>
</joined-subclass>
</hibernate-mapping>


<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0 / / EN "
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

\u0026lt;hibernate-mapping package="main">
\u0026lt;joined-subclass name = "Subcategory2" extends = "Category">
\u0026lt;key column="category_id"/>
\u0026lt;property name="volume"/>
\u0026lt;/ joined-subclass>
\u0026lt;/ hibernate-mapping>


in the database have been created three tables:

 

CREATE TABLE category (category_id
integer NOT NULL, name character varying
(255),
CONSTRAINT category_pkey PRIMARY KEY (category_id)
)

CREATE TABLE subcategory1
(
category_id integer NOT NULL,
size integer,
CONSTRAINT subcategory1_pkey PRIMARY KEY (category_id),
CONSTRAINT fk6c9080d3884719af FOREIGN KEY (category_id)
REFERENCES category (category_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)

CREATE TABLE subcategory2
(
category_id integer NOT NULL,
volume integer,
CONSTRAINT subcategory2_pkey PRIMARY KEY (category_id),
CONSTRAINT fk6c9080d4884719af FOREIGN KEY (category_id)
REFERENCES category (category_id) MATCH
SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION
)


This type of mapping can be issued intuitive polymorphic queries:

 
session.createQuery cats = ("from Category"). List ();
System.out. println (cats.size ()); / / "2"


Each entry in the table and subcategory2 subcategory1 will correspond to an entry in the category table.

Monday, March 9, 2009

Bannedstory Boat Maplestory

representation of inheritance - the first application

I know that this should be the first post, but somehow I did not have the time or desire sklecać everything (all configurations) from the beginning. Well I finally did it and I did the first, a simple application that writes to the database one tuple.

My POJO is very simple:

 
public class Category {
Integer id;
String name;

public Integer getId () {return id
;


} public void setId (Integer id) {
this . id = id;}


public String getName () {return name
;


} public void setName (String name) {this.name = name
;

}}



to this as easy mapping:

 \u0026lt;hibernate-mapping package="main"> 
\u0026lt;class name="Category">
ID> name="id" value="null" column="category_id">
\u0026lt;generator class="sequence">
\u0026lt;/ generator> \u0026lt; , / id> \u0026lt;/ class> \u0026lt;/ hibernate-mapping> \u0026lt;paramname="sequence"> category_seq

\u0026lt;property name="name">
\u0026lt;/ property>


configuration file looks like follows:

 \u0026lt;? xml version = "1.0" encoding = "UTF-8"?> 
\u0026lt;DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/testy
</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver
</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.connection.password">test</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect
</property>
<property name="hibernate.show_sql">false</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="main\Category.hbm.xml" />
</session-factory>
</hibernate-configuration>


is contained in the configuration entry on the automatic creation of database schema-defined mapping files:
 \u0026lt;property name="hbm2ddl.auto"> create \u0026lt;/ property> 


main class of the project is as follows:

 public class 
Main {public static void main (String [] args) {Session session
= null;
SessionFactory SessionFactory = new Configuration (). configure (). buildSessionFactory ();
sessionFactory.openSession session = ();
Category cat = new Category ();
cat.setName ("name");
session.beginTransaction ();
session.save (cat);
session.flush ();
session.getTransaction (). Commit ();
session.close ();
System.out.println ("end");
}}


greatest difficulty can make selecting the correct dependencies. A working configuration is as follows:

- antlr-2.7.6.jar
- backport-util-concurrent-3.1.jar
- postgresql-8.1-407.jdbc3.jar
- ehcache-1.5.0.jar
- log4j-
1.2.14.jar - commons-collections-3.2.jar
- dom4j-1.6.1.jar
- commons-logging-1.1.1.jar
- hibernate-3.2.5.ga.jar
- jta-1.0 .1 B.jar
- xml-apis-1.3.03.jar
- cglib-nodep-2.1_3.jar

Wednesday, March 4, 2009

Windows Aero Template Blog

JUNG. The problem with the generation of various graph output for the same input data.

JUNG stands for Java Universal Network / Graph Framework. Library (or framework, as will the authors) is used in general to build graphs and calculate the coordinates of their vertices. JUNG provides a number of predefined types of graphs: directed, nieskierowane acyclic, with parallel edges, trees, etc.
framework itself is very well udokumentowny and uses it in a very intuitive, so I will confine myself to a brief example:


 private void buildGraph () {
/ / I create a graph
SparseGraph SparseGraph s = new ();

/ / Create the vertices of a graph
UndirectedSparseVertex UndirectedSparseVertex node1 = new ();
UndirectedSparseVertex UndirectedSparseVertex node2 = new ();

/ / add vertices to the graph
g.addVertex (node1);
g.addVertex (node1);


/ / I create the edge between two vertices
UndirectedSparseEdge link = new UndirectedSparseEdge (node1, node2);
/ / add edge to graph
g.addEdge (link);

/ / distribution elements of the graph at the district level, measuring 100 x 100
CircleLayout layout = new MyCircleLayout (g);
layout.initialize (new Dimension (100, 100));

/ / informational Displaying coordinates calculated by Jung
System.out.println ("--------------");
System . out.println ("node1.getX ():" + layout.getLocation (node1). getX ());
System.out.println ("node1.getY ():" + layout.getLocation (node1). getY ());
System.out.println ("node2.getX ():" + layout.getLocation (node2). getX ());
System.out.println ("node2.getY ():" + layout. getLocation (node2). getY ());}

Works!

But not quite as I wanted ... pretty soon it became clear that Jung did not always face the same coordinates for the same graph. If the above method was called twice, the result would look like this: --------------

 
node1 (). GetX (): 10.00
node1 (). GetY (): 20.00
node2 (). getX (): 30.00
node2 (). getY (): 40.00

--------------
node1 (). getX (): 30.00
node1 () . getY (): 40.00
node2 (). getX (): 10.00
node2 (). getY (): 20.00


The problem turned out to be the representation of nodes and edges in the object SparseGraph. These items were kept in the collections of HashSet, Thus the order of their collection of the harvest does not always have (and as it turned out it was not), set! Fast, simple and effective solution to the problem turned out to be writing a class extending SparseGraph and overloaded initialize () method. Finally, the working example, as follows:

 private static class extends MyGraph SparseGraph 
protected void {initialize () {

super.initialize ();
/ / LinkedHashSet ensure that items are placed in the file and downloaded it in the same order
LinkedHashSet mVertices = new ();
LinkedHashSet mEdges = new ();

}} private void

buildGraph () {
/ / I create a graph
MyGraph MyGraph s = new ();

//...
/ / continue the same way as in the previous example
}