Пробуем OpenJPA
authorindvdum
Fri, 12 Aug 2011 16:34:36 +0300
changeset 514e23102379e
parent 4 a7d6344442c6
child 6 a0d8aa779e50
Пробуем OpenJPA
pom.xml
src/main/java/ru/indvdum/mywork/MyWork.java
src/main/java/ru/indvdum/mywork/openjpa/model/DBObject.java
src/main/java/ru/indvdum/mywork/openjpa/model/Day.java
src/main/java/ru/indvdum/mywork/vaadin/MyWorkApplication.java
src/main/resources/META-INF/persistence.xml
     1.1 --- a/pom.xml	Tue Feb 08 15:34:34 2011 +0300
     1.2 +++ b/pom.xml	Fri Aug 12 16:34:36 2011 +0300
     1.3 @@ -9,6 +9,7 @@
     1.4  		<vaadin.version>6.5.0</vaadin.version>
     1.5  		<gwt.version>2.1.1</gwt.version>
     1.6  		<oracle.jdbc.version>10.1.0.5.0</oracle.jdbc.version>
     1.7 +		<openjpa.version>2.0.1</openjpa.version>
     1.8  	</properties>
     1.9  	<repositories>
    1.10  		<repository>
    1.11 @@ -80,6 +81,31 @@
    1.12  					</execution>
    1.13  				</executions>
    1.14  			</plugin>
    1.15 +			<plugin>
    1.16 +				<groupId>org.codehaus.mojo</groupId>
    1.17 +				<artifactId>openjpa-maven-plugin</artifactId>
    1.18 +				<configuration>
    1.19 +					<includes>ru/indvdum/mywork/openjpa/model/**/*.class</includes>
    1.20 +					<addDefaultConstructor>true</addDefaultConstructor>
    1.21 +					<enforcePropertyRestrictions>true</enforcePropertyRestrictions>
    1.22 +				</configuration>
    1.23 +				<executions>
    1.24 +					<execution>
    1.25 +						<id>enhancer</id>
    1.26 +						<phase>process-classes</phase>
    1.27 +						<goals>
    1.28 +							<goal>enhance</goal>
    1.29 +						</goals>
    1.30 +					</execution>
    1.31 +				</executions>
    1.32 +				<dependencies>
    1.33 +					<dependency>
    1.34 +						<groupId>org.apache.openjpa</groupId>
    1.35 +						<artifactId>openjpa</artifactId>
    1.36 +						<version>${openjpa.version}</version>
    1.37 +					</dependency>
    1.38 +				</dependencies>
    1.39 +			</plugin>
    1.40  		</plugins>
    1.41  	</build>
    1.42  	<dependencies>
    1.43 @@ -113,5 +139,10 @@
    1.44  			<artifactId>ojdbc14</artifactId>
    1.45  			<version>${oracle.jdbc.version}</version>
    1.46  		</dependency>
    1.47 +		<dependency>
    1.48 +			<groupId>org.apache.openjpa</groupId>
    1.49 +			<artifactId>openjpa</artifactId>
    1.50 +			<version>${openjpa.version}</version>
    1.51 +		</dependency>
    1.52  	</dependencies>
    1.53  </project>
     2.1 --- a/src/main/java/ru/indvdum/mywork/MyWork.java	Tue Feb 08 15:34:34 2011 +0300
     2.2 +++ b/src/main/java/ru/indvdum/mywork/MyWork.java	Fri Aug 12 16:34:36 2011 +0300
     2.3 @@ -2,19 +2,27 @@
     2.4  
     2.5  import java.util.Date;
     2.6  
     2.7 +import javax.persistence.EntityManager;
     2.8 +import javax.persistence.EntityManagerFactory;
     2.9 +import javax.persistence.Persistence;
    2.10 +
    2.11 +import org.mortbay.component.LifeCycle;
    2.12 +import org.mortbay.component.LifeCycle.Listener;
    2.13  import org.mortbay.jetty.Connector;
    2.14  import org.mortbay.jetty.Server;
    2.15  import org.mortbay.jetty.servlet.Context;
    2.16  import org.mortbay.jetty.servlet.ServletHolder;
    2.17 -import org.mortbay.jetty.webapp.WebAppContext;
    2.18  
    2.19  import com.vaadin.terminal.gwt.server.ApplicationServlet;
    2.20  
    2.21  /**
    2.22   * @author indvdum
    2.23 + * 12.08.2011 17:31:37
    2.24   *
    2.25   */
    2.26  public class MyWork {
    2.27 +	public static EntityManagerFactory EMF = null;
    2.28 +	public static EntityManager EM = null;
    2.29  
    2.30  	/**
    2.31  	 * @param args
    2.32 @@ -32,6 +40,37 @@
    2.33  		vaadinServlet.setInitParameter("application", "ru.indvdum.mywork.vaadin.MyWorkApplication");
    2.34  		root.addServlet(vaadinServlet, "/*");
    2.35  		
    2.36 +		server.addLifeCycleListener(new Listener(){
    2.37 +
    2.38 +			@Override
    2.39 +			public void lifeCycleFailure(LifeCycle event, Throwable cause) {
    2.40 +				
    2.41 +			}
    2.42 +
    2.43 +			@Override
    2.44 +			public void lifeCycleStarted(LifeCycle event) {
    2.45 +				EMF = Persistence.createEntityManagerFactory("mywork");
    2.46 +				EM = EMF.createEntityManager();
    2.47 +			}
    2.48 +
    2.49 +			@Override
    2.50 +			public void lifeCycleStarting(LifeCycle event) {
    2.51 +				
    2.52 +			}
    2.53 +
    2.54 +			@Override
    2.55 +			public void lifeCycleStopped(LifeCycle event) {
    2.56 +				
    2.57 +			}
    2.58 +
    2.59 +			@Override
    2.60 +			public void lifeCycleStopping(LifeCycle event) {
    2.61 +				EM.close();
    2.62 +				EMF.close();
    2.63 +			}
    2.64 +			
    2.65 +		});
    2.66 +		
    2.67  		server.start();
    2.68  		System.out.println("Jetty server started at " + new Date());
    2.69  	}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/main/java/ru/indvdum/mywork/openjpa/model/DBObject.java	Fri Aug 12 16:34:36 2011 +0300
     3.3 @@ -0,0 +1,12 @@
     3.4 +package ru.indvdum.mywork.openjpa.model;
     3.5 +
     3.6 +import java.io.Serializable;
     3.7 +
     3.8 +/**
     3.9 + * @author indvdum
    3.10 + * 12.08.2011 17:31:11
    3.11 + *
    3.12 + */
    3.13 +public interface DBObject extends Serializable {
    3.14 +
    3.15 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/main/java/ru/indvdum/mywork/openjpa/model/Day.java	Fri Aug 12 16:34:36 2011 +0300
     4.3 @@ -0,0 +1,25 @@
     4.4 +package ru.indvdum.mywork.openjpa.model;
     4.5 +
     4.6 +import javax.persistence.Column;
     4.7 +import javax.persistence.Entity;
     4.8 +import javax.persistence.Id;
     4.9 +import javax.persistence.Table;
    4.10 +
    4.11 +/**
    4.12 + * @author indvdum
    4.13 + * 12.08.2011 17:31:00
    4.14 + *
    4.15 + */
    4.16 +@Entity
    4.17 +@Table(name = "DAYS")
    4.18 +public class Day implements DBObject {
    4.19 +
    4.20 +	/**
    4.21 +	 * 
    4.22 +	 */
    4.23 +	private static final long serialVersionUID = 1471705339524132968L;
    4.24 +	
    4.25 +	@Id
    4.26 +	@Column(name = "ID")
    4.27 +	private Integer id;
    4.28 +}
     5.1 --- a/src/main/java/ru/indvdum/mywork/vaadin/MyWorkApplication.java	Tue Feb 08 15:34:34 2011 +0300
     5.2 +++ b/src/main/java/ru/indvdum/mywork/vaadin/MyWorkApplication.java	Fri Aug 12 16:34:36 2011 +0300
     5.3 @@ -1,169 +1,29 @@
     5.4  package ru.indvdum.mywork.vaadin;
     5.5  
     5.6 -import java.sql.SQLException;
     5.7 +import static ru.indvdum.mywork.MyWork.EM;
     5.8 +
     5.9 +import java.util.List;
    5.10 +
    5.11 +import javax.persistence.Query;
    5.12 +
    5.13 +import ru.indvdum.mywork.openjpa.model.Day;
    5.14  
    5.15  import com.vaadin.Application;
    5.16 -import com.vaadin.addon.sqlcontainer.ColumnProperty;
    5.17 -import com.vaadin.addon.sqlcontainer.RowId;
    5.18 -import com.vaadin.addon.sqlcontainer.RowItem;
    5.19 -import com.vaadin.addon.sqlcontainer.SQLContainer;
    5.20 -import com.vaadin.addon.sqlcontainer.connection.JDBCConnectionPool;
    5.21 -import com.vaadin.addon.sqlcontainer.connection.SimpleJDBCConnectionPool;
    5.22 -import com.vaadin.addon.sqlcontainer.query.FreeformQuery;
    5.23 -import com.vaadin.addon.sqlcontainer.query.TableQuery;
    5.24 -import com.vaadin.addon.sqlcontainer.query.generator.OracleGenerator;
    5.25 -import com.vaadin.data.Container;
    5.26 -import com.vaadin.event.ItemClickEvent;
    5.27 -import com.vaadin.event.ItemClickEvent.ItemClickListener;
    5.28 -import com.vaadin.ui.Alignment;
    5.29 -import com.vaadin.ui.Button;
    5.30 -import com.vaadin.ui.Component;
    5.31 -import com.vaadin.ui.DefaultFieldFactory;
    5.32 -import com.vaadin.ui.Field;
    5.33 -import com.vaadin.ui.TabSheet;
    5.34 -import com.vaadin.ui.Table;
    5.35 -import com.vaadin.ui.VerticalLayout;
    5.36  import com.vaadin.ui.Window;
    5.37 -import com.vaadin.ui.Button.ClickEvent;
    5.38 -import com.vaadin.ui.Button.ClickListener;
    5.39  
    5.40  /**
    5.41   * @author indvdum
    5.42 + * 12.08.2011 17:31:53
    5.43   *
    5.44   */
    5.45  public class MyWorkApplication extends Application {
    5.46 -	
    5.47 -	private static JDBCConnectionPool connectionPool = null;
    5.48 -	private RowId taskSelectedRowId = null;
    5.49 -	static{
    5.50 -		try {
    5.51 -			connectionPool = new SimpleJDBCConnectionPool(
    5.52 -					"oracle.jdbc.OracleDriver",
    5.53 -					"jdbc:oracle:thin:@minidev:1521:ora", 
    5.54 -					"velievd", 
    5.55 -					"1", 
    5.56 -					2, 
    5.57 -					2
    5.58 -					);
    5.59 -		} catch (SQLException e) {
    5.60 -			e.printStackTrace();
    5.61 -		}
    5.62 -	}
    5.63  
    5.64  	@Override
    5.65  	public void init() {
    5.66  		Window window = new Window();
    5.67  		setMainWindow(window);
    5.68 -		
    5.69 -		VerticalLayout vlWorkAll = new VerticalLayout();
    5.70 -		VerticalLayout vlTasks = new VerticalLayout();
    5.71 -		VerticalLayout vlDays = new VerticalLayout();
    5.72 -		VerticalLayout vlWork = new VerticalLayout();
    5.73 -		VerticalLayout vlReportForCurrentMonth = new VerticalLayout();
    5.74 -		VerticalLayout vlReportForLastMonth = new VerticalLayout();
    5.75 -		vlWorkAll.setMargin(true);
    5.76 -		vlTasks.setMargin(true);
    5.77 -		vlDays.setMargin(true);
    5.78 -		vlWork.setMargin(true);
    5.79 -		vlReportForCurrentMonth.setMargin(true);
    5.80 -		vlReportForLastMonth.setMargin(true);
    5.81 -		
    5.82 -		TabSheet tabSheet = new TabSheet();
    5.83 -		tabSheet.setSizeFull();
    5.84 -		window.setContent(tabSheet);
    5.85 -		
    5.86 -		tabSheet.addTab(vlWorkAll, "WorkAll", null);
    5.87 -		tabSheet.addTab(vlTasks, "Tasks", null);
    5.88 -		tabSheet.addTab(vlDays, "Days", null);
    5.89 -		tabSheet.addTab(vlWork, "Work", null);
    5.90 -		tabSheet.addTab(vlReportForCurrentMonth, "Report for current month", null);
    5.91 -		tabSheet.addTab(vlReportForLastMonth, "Report for last month", null);
    5.92 -		try {
    5.93 -			Table tWorkAll = new Table("WorkAll", new SQLContainer(new FreeformQuery("select * from myworkall", connectionPool)));
    5.94 -			tWorkAll.setSelectable(true);
    5.95 -			tWorkAll.setImmediate(true);
    5.96 -			tWorkAll.setSizeFull();
    5.97 -			vlWorkAll.addComponent(tWorkAll);
    5.98 -			
    5.99 -			final Table tTasks = new Table("Tasks", new SQLContainer(new TableQuery("tasks", connectionPool, new OracleGenerator())));
   5.100 -			tTasks.setSelectable(true);
   5.101 -			tTasks.setImmediate(true);
   5.102 -			tTasks.setSizeFull();
   5.103 -			vlTasks.addComponent(tTasks);
   5.104 -			final Button taskSave = new Button("Save");
   5.105 -			taskSave.addListener(new ClickListener(){
   5.106 -
   5.107 -				@Override
   5.108 -				public void buttonClick(ClickEvent event) {
   5.109 -					try {
   5.110 -						((SQLContainer)tTasks.getContainerDataSource()).commit();
   5.111 -						tTasks.commit();
   5.112 -					} catch (UnsupportedOperationException e) {
   5.113 -						e.printStackTrace();
   5.114 -					} catch (SQLException e) {
   5.115 -						e.printStackTrace();
   5.116 -					};
   5.117 -					tTasks.setEditable(false);
   5.118 -					taskSave.setVisible(false);
   5.119 -				}
   5.120 -				
   5.121 -			});
   5.122 -			vlTasks.addComponent(taskSave);
   5.123 -			vlTasks.setComponentAlignment(taskSave, Alignment.MIDDLE_RIGHT);
   5.124 -			taskSave.setVisible(false);
   5.125 -			tTasks.addListener(new ItemClickListener(){
   5.126 -
   5.127 -				@Override
   5.128 -				public void itemClick(ItemClickEvent event) {
   5.129 -					if(event.isDoubleClick() && event.getItem() instanceof RowItem){
   5.130 -						tTasks.setEditable(!tTasks.isEditable());
   5.131 -						RowItem item = (RowItem) event.getItem();
   5.132 -						if(item.getItemProperty("ID") instanceof ColumnProperty)
   5.133 -							((ColumnProperty)item.getItemProperty("ID")).setVersionColumn(true);
   5.134 -						taskSelectedRowId = item.getId();
   5.135 -						tTasks.setEditable(true);
   5.136 -						taskSave.setVisible(true);
   5.137 -					}
   5.138 -				}
   5.139 -				
   5.140 -			});
   5.141 -			tTasks.setTableFieldFactory(new DefaultFieldFactory(){
   5.142 -
   5.143 -				@Override
   5.144 -				public Field createField(Container container, Object itemId,
   5.145 -						Object propertyId, Component uiContext) {
   5.146 -					if(itemId == taskSelectedRowId)
   5.147 -						return super.createField(container, itemId, propertyId, uiContext);
   5.148 -					return null;
   5.149 -				}
   5.150 -				
   5.151 -			});
   5.152 -			
   5.153 -			Table tDays = new Table("Days", new SQLContainer(new TableQuery("days", connectionPool, new OracleGenerator())));
   5.154 -			tDays.setSelectable(true);
   5.155 -			tDays.setImmediate(true);
   5.156 -			tDays.setSizeFull();
   5.157 -			vlDays.addComponent(tDays);
   5.158 -			
   5.159 -			Table tWork = new Table("Work", new SQLContainer(new TableQuery("work", connectionPool, new OracleGenerator())));
   5.160 -			tWork.setSelectable(true);
   5.161 -			tWork.setImmediate(true);
   5.162 -			tWork.setSizeFull();
   5.163 -			vlWork.addComponent(tWork);
   5.164 -			
   5.165 -			Table tReportForCurrentMonth = new Table("Report for current month", new SQLContainer(new FreeformQuery("select * from reportforcurrentmonth", connectionPool)));
   5.166 -			tReportForCurrentMonth.setSelectable(true);
   5.167 -			tReportForCurrentMonth.setImmediate(true);
   5.168 -			tReportForCurrentMonth.setSizeFull();
   5.169 -			vlReportForCurrentMonth.addComponent(tReportForCurrentMonth);
   5.170 -			
   5.171 -			Table tReportForLastMonth = new Table("Report for last month", new SQLContainer(new FreeformQuery("select * from reportforlastmonth", connectionPool)));
   5.172 -			tReportForLastMonth.setSelectable(true);
   5.173 -			tReportForLastMonth.setImmediate(true);
   5.174 -			tReportForLastMonth.setSizeFull();
   5.175 -			vlReportForLastMonth.addComponent(tReportForLastMonth);
   5.176 -		} catch (SQLException e) {
   5.177 -			e.printStackTrace();
   5.178 -		}
   5.179 +		Query query = EM.createQuery("select x from " + Day.class.getName() + " x");
   5.180 +		List list = query.getResultList();
   5.181 +		list.size();
   5.182  	}
   5.183  }
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/main/resources/META-INF/persistence.xml	Fri Aug 12 16:34:36 2011 +0300
     6.3 @@ -0,0 +1,17 @@
     6.4 +<?xml version="1.0" encoding="UTF-8"?>
     6.5 +<persistence xmlns="http://java.sun.com/xml/ns/persistence"
     6.6 +	version="1.0">
     6.7 +	<persistence-unit name="mywork">
     6.8 +		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
     6.9 +
    6.10 +		<class>ru.indvdum.mywork.openjpa.model.Day</class>
    6.11 +
    6.12 +		<properties>
    6.13 +			<property name="openjpa.ConnectionPassword" value="1"/>
    6.14 +			<property name="openjpa.ConnectionDriverName" value="oracle.jdbc.OracleDriver"/>
    6.15 +			<property name="openjpa.ConnectionUserName" value="d_mywork"/>
    6.16 +			<property name="openjpa.ConnectionURL" value="jdbc:oracle:thin:@minidev:1521:ora"/>
    6.17 +			<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
    6.18 +		</properties>
    6.19 +	</persistence-unit>
    6.20 +</persistence>