summaryrefslogtreecommitdiff
path: root/orkbasej/java/net/sf/oreka/HibernateManager.java
blob: 806f5ba479c2b117e86ac47fe0f16c98b075e670 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */

/**
 * 
 */
package net.sf.oreka;

import java.io.File;

import net.sf.oreka.persistent.Domain;
import net.sf.oreka.persistent.LoginString;
import net.sf.oreka.persistent.RecPort;
import net.sf.oreka.persistent.RecPortFace;
import net.sf.oreka.persistent.RecProgram;
import net.sf.oreka.persistent.RecSegment;
import net.sf.oreka.persistent.RecSession;
import net.sf.oreka.persistent.RecTape;
import net.sf.oreka.persistent.Service;
import net.sf.oreka.persistent.User;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;


public class HibernateManager {
	
	private static SessionFactory sessionFactory = null;
	
	public static void configure(String filename) throws Exception {
		
		File configFile = new File(filename);
		
		AnnotationConfiguration config = new AnnotationConfiguration();
		config.configure(configFile);
		
		config.addAnnotatedClass(RecProgram.class);
		config.addAnnotatedClass(RecSession.class);
		config.addAnnotatedClass(RecSegment.class);
		config.addAnnotatedClass(RecTape.class);
		config.addAnnotatedClass(User.class);
		config.addAnnotatedClass(LoginString.class);
		config.addAnnotatedClass(Domain.class);		
		config.addAnnotatedClass(Service.class);
		config.addAnnotatedClass(RecPort.class);
		config.addAnnotatedClass(RecPortFace.class);
		sessionFactory = config.buildSessionFactory();
	}
	
	public static Session getSession() throws Exception {
		if (sessionFactory == null) {
			throw new OrkException("HibernateManager.getSession: application must configure hibernate before using it.");
		}
		return sessionFactory.openSession();
	}
}