@Override @Nullable public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { // Pass custom isolation level on to EclipseLink's DatabaseLogin configuration // (since Spring 4.1.2) UnitOfWork uow = entityManager.unwrap(UnitOfWork.class); uow.getLogin().setTransactionIsolation(definition.getIsolationLevel()); } entityManager.getTransaction().begin(); if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) { // Begin an early transaction to force EclipseLink to get a JDBC Connection // so that Spring can manage transactions with JDBC as well as EclipseLink. entityManager.unwrap(UnitOfWork.class).beginEarlyTransaction(); } return null; }
DatabaseRecord recordWithValues= new DatabaseRecord(); recordWithValues.add(new DatabaseField("param1"), "someValue"); String sqlStringWithArgs = databaseQuery.getTranslatedSQLString(session, recordWithValues);
entityManager.getTransaction().begin(); UnitOfWork unitOfWork = (UnitOfWork)((JpaEntityManager)entityManager.getDelegate()).getActiveSession(); unitOfWork.beginEarlyTransaction(); Accessor accessor = unitOfWork.getAccessor(); accessor.incrementCallCount(unitOfWork.getParent()); accessor.decrementCallCount(); java.sql.Connection connection = accessor.getConnection(); ... entityManager.getTransaction().commit();
/** * INTERNAL: * Start call */ protected void startOperationProfile() { if (getSession().isInProfile()) { getSession().getProfiler().startOperationProfile(SessionProfiler.SessionEvent); } }
/** * INTERNAL: * Connect to the datasource, and return the driver level connection object. */ @Override public Object connectToDatasource(Accessor accessor, Session session) throws DatabaseException { return getConnector().connect(prepareProperties(properties), session); }
/** * PUBLIC: * Specify the J2EE DataSource name to connect to. * Also enable external connection pooling. * @see JNDIConnector */ public void useDataSource(String dataSource) { setConnector(new JNDIConnector(dataSource)); useExternalConnectionPooling(); }
final Map<Class, ClassDescriptor> descriptors = session.getDescriptors(); final ClassDescriptor descriptor = session.getDescriptor(expectedType);
/** * PUBLIC: * The database URL is the JDBC URL for the database server. * The driver header is <i>not</i> be included in this URL * (e.g. "dbase files"; not "jdbc:odbc:dbase files"). */ public String getDatabaseURL() { if (!(getConnector() instanceof DefaultConnector)) { return ""; } return getDefaultConnector().getDatabaseURL(); }
/** * PUBLIC: * Connect to the JDBC driver via DriverManager. * @see #useDirectDriverConnect(String, String, String) */ public void useDefaultDriverConnect(String driverClassName, String driverURLHeader, String databaseURL) { setConnector(new DefaultConnector(driverClassName, driverURLHeader, databaseURL)); }
/** * PUBLIC: * Some JDBC drivers don't support connecting correctly (via DriverManager), * but do support connecting incorrectly (e.g. Castanet). * @see #useDefaultDriverConnect(String, String, String) */ public void useDirectDriverConnect(String driverClassName, String driverURLHeader, String databaseURL) { setConnector(new DirectConnector(driverClassName, driverURLHeader, databaseURL)); }
Query query = em.createNamedQuery("findMe"); Session session = em.unwrap(JpaEntityManager.class).getActiveSession(); DatabaseQuery databaseQuery = ((EJBQueryImpl)query).getDatabaseQuery(); databaseQuery.prepareCall(session, new DatabaseRecord()); String sqlString = databaseQuery.getSQLString();
/** * PUBLIC: * The database URL is the JDBC URL for the database server. * The driver header should <i>not</i> be included in this URL * (e.g. "dbase files"; not "jdbc:odbc:dbase files"). */ public void setDatabaseURL(String databaseURL) throws ValidationException { getDefaultConnector().setDatabaseURL(databaseURL); }
/** * PUBLIC: * The driver URL header is the string predetermined by the JDBC driver to be * part of the URL connection string, (e.g. "jdbc:odbc:"). * This is required to connect to the database. */ public void setDriverURLHeader(String driverURLHeader) throws ValidationException { getDefaultConnector().setDriverURLHeader(driverURLHeader); }
import org.eclipse.persistence.descriptors.ClassDescriptor; import org.eclipse.persistence.mappings.DatabaseMapping; import org.eclipse.persistence.oxm.mappings.XMLDirectMapping; import org.eclipse.persistence.oxm.mappings.nullpolicy.XMLNullRepresentationType; import org.eclipse.persistence.sessions.*; public class NullPolicySessionEventListener extends SessionEventAdapter { @Override public void preLogin(SessionEvent event) { Project project = event.getSession().getProject(); for(ClassDescriptor descriptor : project.getOrderedDescriptors()) { for(DatabaseMapping mapping : descriptor.getMappings()) { if(mapping.isAbstractDirectMapping()) { XMLDirectMapping xmlDirectMapping = (XMLDirectMapping) mapping; xmlDirectMapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.EMPTY_NODE); xmlDirectMapping.getNullPolicy().setNullRepresentedByEmptyNode(true); } } } } }
import org.eclipse.persistence.config.SessionCustomizer; import org.eclipse.persistence.sessions.Session; import org.eclipse.persistence.sessions.SessionEvent; import org.eclipse.persistence.sessions.SessionEventAdapter; import org.eclipse.persistence.sessions.UnitOfWork; public class ImportSQL implements SessionCustomizer { private void importSql(UnitOfWork unitOfWork, String fileName) { // Open file // Execute each line, e.g., // unitOfWork.executeNonSelectingSQL("select 1 from dual"); } @Override public void customize(Session session) throws Exception { session.getEventManager().addListener(new SessionEventAdapter() { @Override public void postLogin(SessionEvent event) { String fileName = (String) event.getSession().getProperty("import.sql.file"); UnitOfWork unitOfWork = event.getSession().acquireUnitOfWork(); importSql(unitOfWork, fileName); unitOfWork.commit() } }); }
/** * INTERNAL: * Start call */ protected void startOperationProfile() { if (getSession().isInProfile()) { getSession().getProfiler().startOperationProfile(SessionProfiler.SessionEvent); } }
/** * INTERNAL: * Connect to the datasource, and return the driver level connection object. */ public Object connectToDatasource(Accessor accessor, Session session) throws DatabaseException { return getConnector().connect(prepareProperties(properties), session); }
@Override @Nullable public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { // Pass custom isolation level on to EclipseLink's DatabaseLogin configuration // (since Spring 4.1.2) UnitOfWork uow = entityManager.unwrap(UnitOfWork.class); uow.getLogin().setTransactionIsolation(definition.getIsolationLevel()); } entityManager.getTransaction().begin(); if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) { // Begin an early transaction to force EclipseLink to get a JDBC Connection // so that Spring can manage transactions with JDBC as well as EclipseLink. entityManager.unwrap(UnitOfWork.class).beginEarlyTransaction(); } return null; }
/** * PUBLIC: * Connect to the JDBC driver via DriverManager. * @see #useDirectDriverConnect() */ public void useDefaultDriverConnect() { setConnector(new DefaultConnector()); }
/** * PUBLIC: * Some JDBC drivers don't support connecting correctly (via DriverManager), * but do support connecting incorrectly (e.g. Castanet). * @see #useDirectDriverConnect() */ public void useDirectDriverConnect() { setConnector(new DirectConnector()); }