Skip to content

Commit

Permalink
Merge pull request #1 from A-Kastner/patch-1
Browse files Browse the repository at this point in the history
Update README.md
  • Loading branch information
long2mao1 authored Nov 19, 2018
2 parents 26872ea + aee009b commit adef8a2
Showing 1 changed file with 32 additions and 76 deletions.
108 changes: 32 additions & 76 deletions contributions/hibernateAnnotations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ In addition to the wiki-listed developers and reviewers, these sources are ackno

# Contribution's Documentation Changes

== Headline ==
== Headline ==

[[Object-Relational mapping]] for [[Language:Java|Java]] with [[Technology:Hibernate|Hibernate]] and [[Technology:JPA]].

== Characteristics ==
== Characteristics ==

The contribution demonstrates [[Object-Relational mapping]] or [[persistence]] on the Java platform. A mapping is definied in the object models as Java-Annotations.

== Relationships ==

[[Contribution:NAME|Contribution:NAME]] is a variation of [[Contribution:hibernate|hibernate]]. The variation is concerned with the realization of [[O/R mapping]]. That is, this project's object model is mapped by use of [[Technology:JPA]] annotations and used by the [[Technology:Hibernate|Hibernate framework]] instead of legacy [[Language:XML]]-mapping files for all [[Persistence|persistent]] classes.
[[Contribution:hibernateAnnotations|Contribution:hibernateAnnotations]] is a variation of [[Contribution:hibernateJava10|hibernateJava10]]. The variation is concerned with the realization of [[O/R mapping]]. That is, this project's object model is mapped by use of [[Technology:JPA]] annotations and used by the [[Technology:Hibernate|Hibernate framework]] instead of legacy [[Language:XML]]-mapping files for all [[Persistence|persistent]] classes.

== Illustration ==

Expand All @@ -32,35 +32,35 @@ Consider the following sketch of the class for departments:
@Table(name="DEPARTMENT")
public class Department {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private Long id;

@Column(name="NAME")
private String name;

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="DEPT_ID")
private Set<Employee> employees;

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="DEPT_ID")
private Set<Department> subdepts;

public Long getId() { ... }
private void setId(Long id) { ... }
public String getName() { ... }
public void setName(String name) { ... }
public Set<Employee> getEmployees() { ... }
private void setEmployees(Set<Employee> employees) { ... }
public Set<Department> getSubdepts() { ... }
private void setSubdepts(Set<Department> subdepts) { ... }
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private Long id;
@Column(name="NAME")
private String name;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="DEPT_ID")
private Set<Employee> employees;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="DEPT_ID")
private Set<Department> subdepts;

public Long getId() { ... }
private void setId(Long id) { ... }
public String getName() { ... }
public void setName(String name) { ... }
public Set<Employee> getEmployees() { ... }
private void setEmployees(Set<Employee> employees) { ... }
public Set<Department> getSubdepts() { ... }
private void setSubdepts(Set<Department> subdepts) { ... }
}
</syntaxhighlight>

Each persistent class must provide a property (i.e., a getter and a setter) for
an ''id'' that can serve as primary key in the database. Properties of
an ''id'' that can serve as primary key in the database. Properties of
collection types (in fact, set types) proxy for one-to-many relationships.
Annotations are added to associate classes with a relational schema.
They can be used in addition to, but preferably as a replacement of XML mapping metadata.
Expand All @@ -78,57 +78,13 @@ That is, the ''id'' property is directly mapped to a primary key column
by use of @Id. @GeneratedValue allows the application to set
the property itself, i.e. generate some identifier for your entity.

The ''name'' property is mapped to column ''NAME'' by the optional @Column annotation.
Further, the ''employees''
property is non-trivially associated with a foreign key ''DEPT_ID'' of the
property is non-trivially associated with a foreign key ''DEPT_ID'' of the
''EMPLOYEE'' table by use of the @OneToMany annotation. This is only
indirectly identified through the mentioning of the ''Employee'' class; likewise for sub-departments.

Persistent objects are brought back to life as follows:

<syntaxhighlight lang="java">
public Company loadCompany(String name) {
this.session = getSessionFactory().getCurrentSession();
this.session.beginTransaction();
List<?> result = this.session.createQuery(
"from Company where name = '" + name + "'").list();
for (Object o : result)
return (Company)o;
return null;
}
</syntaxhighlight>

That is, an [[Language:HQL]] query is executed to retrieve a company
that is identified by name; the Hibernate framework takes care of populating
the company object and all its sub-objects. If the requested company cannot be found,
then ''null'' is returned.

Finally, consider the Hibernate configuration:

<syntaxhighlight lang="xml">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings. -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost:9001</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- Create the database schema, if needed; update otherwise -->
<property name="hbm2ddl.auto">update</property>
<!-- mappings for annotated classes -->
<mapping class="org.softlang.company.Employee"/>
<mapping class="org.softlang.company.Department"/>
<mapping class="org.softlang.company.Company"/>
...
</session-factory>
</hibernate-configuration>
</syntaxhighlight>

This configuration helps the runtime to connect to the right database, to find
all mapping files of interest, and to define some essential settings. For instance,
Hibernate is informed here that the database catalog is to be updated automatically
(see ''hbm2ddl.auto'' ... ''update'') upon starting a Hibernate session. In particular,
if the mapping-implied tables are not yet declared in the database, then they will
be created automatically.
For further Illustration refer to [[Contribution:hibernate]]

== Usage ==

Expand Down Expand Up @@ -180,7 +136,7 @@ Several test cases are collected in package org.softlang.tests:
'''Finish off'''
Just execute *stop* and quit the database monitor.

== Metadata ==
== Metadata ==

* [[memberOf::Theme:Java mapping]]
* [[uses::Language:Java]]
Expand Down

0 comments on commit adef8a2

Please sign in to comment.