Skip to end of metadata
Go to start of metadata

Introduction

 

The plug-in mechanism provides tools that allow plug-ins to use the database. The recommended method is to use the Hibernate library.

The plug-in can get access to any database, but this chapter will describe only the system's database support.


Objects configuration

Chapter Plugin context configuration describes how to add beany to the context. The database support will be based on the integration of SpringFramework and HibernateThe XMLconfiguration will be described.

To register the SessionFactory, just add such a bean declaration. The object automatically configures the used dialect, datasource etc.

plugin-context.xml
<bean id="sessionFactory" class="com.suncode.pwfl.persistence.support.hibernate.SystemBaseSessionFactoryBean">
	<property name="packagesToScan" value="com.suncode.plugin.tutorial" />
</bean>

Class com.suncode.pwfl.persistence.support.hibernate.SystemBaseSessionFactoryBean is based on class LocalSessionFactoryBean. Documentation i salso located there.

 

We probably want to use other mechanisms that simplify the transaction code:

plugin-context.xml
<!-- adnotacje @Transactional -->
<tx:annotation-driven/>
 
<!-- TransactionManager -->
<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
	<property name="sessionFactory" ref="sessionFactory" />
</bean>

Usage example 

 Now create our entity:

Note
@Entity
@Table(name = "pm_tutorial_note")
@SequenceGenerator(name = "note", sequenceName = "pm_tutorial_note_id")
public class Note {
 
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "note")
	private Long id;
 
	@Column(length = 2000)
	private String text;
 
	// getters...
	// setters...
}

Names of tables, columns and sequencers should not exceed 30 characters (Oracle limit).

 

Saving and reading of our object may look like this:

NoteService
@Service
@Transactional
public class NoteService {
 
	@Autowired
	private SessionFactory sessionFactory;
 
	public void saveNote(Note note){
		sessionFactory.getCurrentSession().save(note);
	}
 
	public Note getNote(Long id){
		return (Note) sessionFactory.getCurrentSession().get(Note.class, id);
	}
}

Any changes made to the plugin do not require a server restart. Even the hibernate mapping changes are visible after updating the plugin.

Use of system entities in mapping 

It may happen that in our entity we need mapping, eg for the system user. In this case, we need to import the user mapping so that our SessionFactory instance has all the information needed to write into the database.

The import of mappings from the system takes place during the configuration of the Hibernate SessionFactory:

plugin-context.xml
<bean id="sessionFactory" class="com.suncode.pwfl.persistence.support.hibernate.SystemBaseSessionFactoryBean">
	<property name="packagesToScan" value="com.suncode.plugin.tutorial" />
	
	<!-- We need to import the entire dependency tree -->
	<property name="importedMappings">
		<array>
			<value>com.suncode.pwfl.administration.user.User</value>
			<value>com.suncode.pwfl.administration.user.UserGroup</value>
			<value>com.suncode.pwfl.administration.structure.Position</value>
			<value>com.suncode.pwfl.administration.structure.OrganizationalUnit</value>
			<value>com.suncode.pwfl.administration.structure.Role</value>
		</array>
	</property>
</bean>

Currently, we have to write all dependencies, even transient ones. If we want to import only the user, we need to import another 4 mappings as shown above.

It is planned to improve the mapping import method.

 

Our updated entity:

Note
@Entity
@Table(name = "pm_tutorial_note")
@SequenceGenerator(name = "note", sequenceName = "pm_tutorial_note_id")
public class Note {
 
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "note")
	private Long id;
 
	@Column(length = 2000)
	private String text;
	
	@ManyToOne
    @JoinColumn(name = "user_id")
    @OnDelete(action = OnDeleteAction.CASCADE)
	private User user;
 
	// getters...
	// setters...
}

Adding a user object to our entity will result in adding a foreign key in the database schema. Unfortunately, in this case it will not be possible to remove the user from the system until all his notes have been deleted.

Annotation:

@OnDelete(action = OnDeleteAction.CASCADE)

Adds cascading delete at the database schema level (unlike the cascade annotation option), so that when deleting a user, all of his notes will be deleted.

There is a need for a different solution to this problem, but currently this is the only option. 

 

 

  • No labels

1 Comment

  1. W Svn w lokalizacji http://192.168.1.51/svn/PluginsExample/dbexample scomitowałem prostą przykładową wtyczkę służącą do utworzenia tabeli pm_plugin_dbtest i wstawieniu do niej rekordu, wtyczka uruchamiana za pomocą linku

    np. http://localhost:8080/plusworkflow-wtyczki/plugin/com.suncode.plugin-dbexample/hello   , wtyczka oparta o mechanizm adnotacji Springa do połączenia z bazą.

    Obsługa bazy została napisana w wtyczce na podstawie instrukcji systemowej (Zarządzanie własnymi obiektami bazodanowymi).

    Wtyczka po uruchomieniu dodaje jeden wiersz do tabeli i wyświetla prostą stronę z tekstem "Hello".