6 配置和启动时容器和提供者之间的协议
6.3 J AVAX . PERSISTENCE . SPI .P ERSISTENCE U NIT I NFO 接口
import javax.sql.DataSource;
/**
* Interface implemented by the container and used by the * persistence provider when creating an EntityManagerFactory.
*/
public interface PersistenceUnitInfo { /**
* @return The name of the persistence unit.
*Correspondstothenameattributeinthepersistence.xmlfile.
*/
public String getPersistenceUnitName();
/**
* @return The fully qualified name of the persistence provider * implementation class.
* Corresponds to the <provider> element in the persistence.xml * file.
*/
public String getPersistenceProviderClassName();
/**
* @return The transaction type of the entity managers created * by the EntityManagerFactory.
* The transaction type corresponds to the transaction-type * attribute in the persistence.xml file.
*/
public PersistenceUnitTransactionType getTransactionType();
/**
* @return The JTA-enabled data source to be used by the * persistence provider.
* The data source corresponds to the <jta-data-source>
* element in the persistence.xml file or is provided at * deployment or by the container.
*/
public DataSource getJtaDataSource();
/**
* @return The non-JTA-enabled data source to be used by the * persistence provider for accessing data outside a JTA
* transaction.
*Thedatasourcecorrespondstothenamed<non-jta-data-source>
* element in the persistence.xml file or provided at * deployment or by the container.
*/
public DataSource getNonJtaDataSource();
/**
* @return The list of mapping file names that the persistence * provider must load to determine the mappings for the entity * classes. The mapping files must be in the standard XML * mapping format, be uniquely named and be resource-loadable * from the application classpath.
* Each mapping file name corresponds to a <mapping-file>
* element in the persistence.xml file.
*/
public List<String> getMappingFileNames();
/**
* Returns a list of URLs for the jar files or exploded jar * file directories that the persistence provider must examine * for managed classes of the persistence unit. Each URL * corresponds to a named <jar-file> element in the * persistence.xml file. A URL will either be a file:
* URL referring to a jar file or referring to a directory * that contains an exploded jar file, or some other URL from * which an InputStream in jar format can be obtained.
*
* @return a list of URL objects referring to jar files or * directories.
*/
public List<URL> getJarFileUrls();
/**
* Returns the URL for the jar file or directory that is the * root of the persistence unit. (If the persistence unit is * rooted in the WEB-INF/classes directory, this will be the
* URL of that directory.)
* The URL will either be a file: URL referring to a jar file * or referring to a directory that contains an exploded jar * file, or some other URL from which an InputStream in jar * format can be obtained.
*
* @return a URL referring to a jar file or directory.
*/
public URL getPersistenceUnitRootUrl();
/**
* @return The list of the names of the classes that the * persistence provider must add it to its set of managed
* classes. Each name corresponds to a named <class> element * in the persistence.xml file.
*/
public List<String> getManagedClassNames();
/**
* @return Whether classes in the root of the persistence * unit that have not been explicitly listed are to be * included in the set of managed classes.
* This value corresponds to the <exclude-unlisted-classes>
* element in the persistence.xml file.
*/
public boolean excludeUnlistedClasses();
/**
* @return Properties object. Each property corresponds * to a <property> element in the persistence.xml file */
public Properties getProperties();
/**
* @return ClassLoader that the provider may use to load any * classes, resources, or open URLs.
*/
public ClassLoader getClassLoader();
/**
* Add a transformer supplied by the provider that will be * called for every new class definition or class redefinition * that gets loaded by the loader returned by the
* PersistenceUnitInfo.getClassLoader method. The transformer * has no effect on the result returned by the
* PersistenceUnitInfo.getNewTempClassLoader method.
*Classesareonlytransformedoncewithinthesameclassloading * scope, regardless of how many persistence units they may be * a part of.
*
* @param transformer A provider-supplied transformer that the * Container invokes at class-(re)definition time
*/
public void addTransformer(ClassTransformer transformer);
/**
* Return a new instance of a ClassLoader that the provider * may use to temporarily load any classes, resources, or * open URLs. The scope and classpath of this loader is * exactly the same as that of the loader returned by
*PersistenceUnitInfo.getClassLoader.Noneoftheclassesloaded * by this class loader will be visible to application
* components. The provider may only use this ClassLoader * within the scope of the createContainerEntityManagerFactory * call.
*
* @return Temporary ClassLoader with same visibility as current * loader
*/
public ClassLoader getNewTempClassLoader();
}
Theenum javax.persistence.spi.PersistenceUnitTransactionType de?nes
whether the entity managers created by the factory will be JTA or resource-local entity managers.
public enum PersistenceUnitTransactionType { JTA,
* A persistence provider supplies an instance of this * interface to the PersistenceUnitInfo.addTransformer * method. The supplied transformer instance will get * called to transform entity class files when they are * loaded or redefined. The transformation occurs before * the class is defined by the JVM.
*/
public interface ClassTransformer { /**
* Invoked when a class is being loaded or redefined.
* The implementation of this method may transform the * supplied class file and return a new replacement class * file.
*
* @param loader The defining loader of the class to be * transformed, may be null if the bootstrap loader
* @param className The name of the class in the internal form * of fully qualified class and interface names
* @param classBeingRedefined If this is a redefine, the * class being redefined, otherwise null
* @param protectionDomain The protection domain of the * class being defined or redefined
* @param classfileBuffer The input byte buffer in class * file format - must not be modified
* @return A well-formed class file buffer (the result of * the transform), or null if no transform is performed * @throws IllegalClassFormatException If the input does * not represent a well-formed class file
*/
byte[] transform(ClassLoader loader, String className,
Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer)
throws IllegalClassFormatException;
}