# HG changeset patch # User indvdum # Date 1313156076 -10800 # Node ID 14e23102379e324f572dff38597ddb55654b606b # Parent a7d6344442c614f6e652df302e98bec5482580b1 Пробуем OpenJPA diff -r a7d6344442c6 -r 14e23102379e pom.xml --- a/pom.xml Tue Feb 08 15:34:34 2011 +0300 +++ b/pom.xml Fri Aug 12 16:34:36 2011 +0300 @@ -9,6 +9,7 @@ 6.5.0 2.1.1 10.1.0.5.0 + 2.0.1 @@ -80,6 +81,31 @@ + + org.codehaus.mojo + openjpa-maven-plugin + + ru/indvdum/mywork/openjpa/model/**/*.class + true + true + + + + enhancer + process-classes + + enhance + + + + + + org.apache.openjpa + openjpa + ${openjpa.version} + + + @@ -113,5 +139,10 @@ ojdbc14 ${oracle.jdbc.version} + + org.apache.openjpa + openjpa + ${openjpa.version} + diff -r a7d6344442c6 -r 14e23102379e src/main/java/ru/indvdum/mywork/MyWork.java --- a/src/main/java/ru/indvdum/mywork/MyWork.java Tue Feb 08 15:34:34 2011 +0300 +++ b/src/main/java/ru/indvdum/mywork/MyWork.java Fri Aug 12 16:34:36 2011 +0300 @@ -2,19 +2,27 @@ import java.util.Date; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import org.mortbay.component.LifeCycle; +import org.mortbay.component.LifeCycle.Listener; import org.mortbay.jetty.Connector; import org.mortbay.jetty.Server; import org.mortbay.jetty.servlet.Context; import org.mortbay.jetty.servlet.ServletHolder; -import org.mortbay.jetty.webapp.WebAppContext; import com.vaadin.terminal.gwt.server.ApplicationServlet; /** * @author indvdum + * 12.08.2011 17:31:37 * */ public class MyWork { + public static EntityManagerFactory EMF = null; + public static EntityManager EM = null; /** * @param args @@ -32,6 +40,37 @@ vaadinServlet.setInitParameter("application", "ru.indvdum.mywork.vaadin.MyWorkApplication"); root.addServlet(vaadinServlet, "/*"); + server.addLifeCycleListener(new Listener(){ + + @Override + public void lifeCycleFailure(LifeCycle event, Throwable cause) { + + } + + @Override + public void lifeCycleStarted(LifeCycle event) { + EMF = Persistence.createEntityManagerFactory("mywork"); + EM = EMF.createEntityManager(); + } + + @Override + public void lifeCycleStarting(LifeCycle event) { + + } + + @Override + public void lifeCycleStopped(LifeCycle event) { + + } + + @Override + public void lifeCycleStopping(LifeCycle event) { + EM.close(); + EMF.close(); + } + + }); + server.start(); System.out.println("Jetty server started at " + new Date()); } diff -r a7d6344442c6 -r 14e23102379e src/main/java/ru/indvdum/mywork/openjpa/model/DBObject.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/ru/indvdum/mywork/openjpa/model/DBObject.java Fri Aug 12 16:34:36 2011 +0300 @@ -0,0 +1,12 @@ +package ru.indvdum.mywork.openjpa.model; + +import java.io.Serializable; + +/** + * @author indvdum + * 12.08.2011 17:31:11 + * + */ +public interface DBObject extends Serializable { + +} diff -r a7d6344442c6 -r 14e23102379e src/main/java/ru/indvdum/mywork/openjpa/model/Day.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/ru/indvdum/mywork/openjpa/model/Day.java Fri Aug 12 16:34:36 2011 +0300 @@ -0,0 +1,25 @@ +package ru.indvdum.mywork.openjpa.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * @author indvdum + * 12.08.2011 17:31:00 + * + */ +@Entity +@Table(name = "DAYS") +public class Day implements DBObject { + + /** + * + */ + private static final long serialVersionUID = 1471705339524132968L; + + @Id + @Column(name = "ID") + private Integer id; +} diff -r a7d6344442c6 -r 14e23102379e src/main/java/ru/indvdum/mywork/vaadin/MyWorkApplication.java --- a/src/main/java/ru/indvdum/mywork/vaadin/MyWorkApplication.java Tue Feb 08 15:34:34 2011 +0300 +++ b/src/main/java/ru/indvdum/mywork/vaadin/MyWorkApplication.java Fri Aug 12 16:34:36 2011 +0300 @@ -1,169 +1,29 @@ package ru.indvdum.mywork.vaadin; -import java.sql.SQLException; +import static ru.indvdum.mywork.MyWork.EM; + +import java.util.List; + +import javax.persistence.Query; + +import ru.indvdum.mywork.openjpa.model.Day; import com.vaadin.Application; -import com.vaadin.addon.sqlcontainer.ColumnProperty; -import com.vaadin.addon.sqlcontainer.RowId; -import com.vaadin.addon.sqlcontainer.RowItem; -import com.vaadin.addon.sqlcontainer.SQLContainer; -import com.vaadin.addon.sqlcontainer.connection.JDBCConnectionPool; -import com.vaadin.addon.sqlcontainer.connection.SimpleJDBCConnectionPool; -import com.vaadin.addon.sqlcontainer.query.FreeformQuery; -import com.vaadin.addon.sqlcontainer.query.TableQuery; -import com.vaadin.addon.sqlcontainer.query.generator.OracleGenerator; -import com.vaadin.data.Container; -import com.vaadin.event.ItemClickEvent; -import com.vaadin.event.ItemClickEvent.ItemClickListener; -import com.vaadin.ui.Alignment; -import com.vaadin.ui.Button; -import com.vaadin.ui.Component; -import com.vaadin.ui.DefaultFieldFactory; -import com.vaadin.ui.Field; -import com.vaadin.ui.TabSheet; -import com.vaadin.ui.Table; -import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.Window; -import com.vaadin.ui.Button.ClickEvent; -import com.vaadin.ui.Button.ClickListener; /** * @author indvdum + * 12.08.2011 17:31:53 * */ public class MyWorkApplication extends Application { - - private static JDBCConnectionPool connectionPool = null; - private RowId taskSelectedRowId = null; - static{ - try { - connectionPool = new SimpleJDBCConnectionPool( - "oracle.jdbc.OracleDriver", - "jdbc:oracle:thin:@minidev:1521:ora", - "velievd", - "1", - 2, - 2 - ); - } catch (SQLException e) { - e.printStackTrace(); - } - } @Override public void init() { Window window = new Window(); setMainWindow(window); - - VerticalLayout vlWorkAll = new VerticalLayout(); - VerticalLayout vlTasks = new VerticalLayout(); - VerticalLayout vlDays = new VerticalLayout(); - VerticalLayout vlWork = new VerticalLayout(); - VerticalLayout vlReportForCurrentMonth = new VerticalLayout(); - VerticalLayout vlReportForLastMonth = new VerticalLayout(); - vlWorkAll.setMargin(true); - vlTasks.setMargin(true); - vlDays.setMargin(true); - vlWork.setMargin(true); - vlReportForCurrentMonth.setMargin(true); - vlReportForLastMonth.setMargin(true); - - TabSheet tabSheet = new TabSheet(); - tabSheet.setSizeFull(); - window.setContent(tabSheet); - - tabSheet.addTab(vlWorkAll, "WorkAll", null); - tabSheet.addTab(vlTasks, "Tasks", null); - tabSheet.addTab(vlDays, "Days", null); - tabSheet.addTab(vlWork, "Work", null); - tabSheet.addTab(vlReportForCurrentMonth, "Report for current month", null); - tabSheet.addTab(vlReportForLastMonth, "Report for last month", null); - try { - Table tWorkAll = new Table("WorkAll", new SQLContainer(new FreeformQuery("select * from myworkall", connectionPool))); - tWorkAll.setSelectable(true); - tWorkAll.setImmediate(true); - tWorkAll.setSizeFull(); - vlWorkAll.addComponent(tWorkAll); - - final Table tTasks = new Table("Tasks", new SQLContainer(new TableQuery("tasks", connectionPool, new OracleGenerator()))); - tTasks.setSelectable(true); - tTasks.setImmediate(true); - tTasks.setSizeFull(); - vlTasks.addComponent(tTasks); - final Button taskSave = new Button("Save"); - taskSave.addListener(new ClickListener(){ - - @Override - public void buttonClick(ClickEvent event) { - try { - ((SQLContainer)tTasks.getContainerDataSource()).commit(); - tTasks.commit(); - } catch (UnsupportedOperationException e) { - e.printStackTrace(); - } catch (SQLException e) { - e.printStackTrace(); - }; - tTasks.setEditable(false); - taskSave.setVisible(false); - } - - }); - vlTasks.addComponent(taskSave); - vlTasks.setComponentAlignment(taskSave, Alignment.MIDDLE_RIGHT); - taskSave.setVisible(false); - tTasks.addListener(new ItemClickListener(){ - - @Override - public void itemClick(ItemClickEvent event) { - if(event.isDoubleClick() && event.getItem() instanceof RowItem){ - tTasks.setEditable(!tTasks.isEditable()); - RowItem item = (RowItem) event.getItem(); - if(item.getItemProperty("ID") instanceof ColumnProperty) - ((ColumnProperty)item.getItemProperty("ID")).setVersionColumn(true); - taskSelectedRowId = item.getId(); - tTasks.setEditable(true); - taskSave.setVisible(true); - } - } - - }); - tTasks.setTableFieldFactory(new DefaultFieldFactory(){ - - @Override - public Field createField(Container container, Object itemId, - Object propertyId, Component uiContext) { - if(itemId == taskSelectedRowId) - return super.createField(container, itemId, propertyId, uiContext); - return null; - } - - }); - - Table tDays = new Table("Days", new SQLContainer(new TableQuery("days", connectionPool, new OracleGenerator()))); - tDays.setSelectable(true); - tDays.setImmediate(true); - tDays.setSizeFull(); - vlDays.addComponent(tDays); - - Table tWork = new Table("Work", new SQLContainer(new TableQuery("work", connectionPool, new OracleGenerator()))); - tWork.setSelectable(true); - tWork.setImmediate(true); - tWork.setSizeFull(); - vlWork.addComponent(tWork); - - Table tReportForCurrentMonth = new Table("Report for current month", new SQLContainer(new FreeformQuery("select * from reportforcurrentmonth", connectionPool))); - tReportForCurrentMonth.setSelectable(true); - tReportForCurrentMonth.setImmediate(true); - tReportForCurrentMonth.setSizeFull(); - vlReportForCurrentMonth.addComponent(tReportForCurrentMonth); - - Table tReportForLastMonth = new Table("Report for last month", new SQLContainer(new FreeformQuery("select * from reportforlastmonth", connectionPool))); - tReportForLastMonth.setSelectable(true); - tReportForLastMonth.setImmediate(true); - tReportForLastMonth.setSizeFull(); - vlReportForLastMonth.addComponent(tReportForLastMonth); - } catch (SQLException e) { - e.printStackTrace(); - } + Query query = EM.createQuery("select x from " + Day.class.getName() + " x"); + List list = query.getResultList(); + list.size(); } } diff -r a7d6344442c6 -r 14e23102379e src/main/resources/META-INF/persistence.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/resources/META-INF/persistence.xml Fri Aug 12 16:34:36 2011 +0300 @@ -0,0 +1,17 @@ + + + + org.apache.openjpa.persistence.PersistenceProviderImpl + + ru.indvdum.mywork.openjpa.model.Day + + + + + + + + + +