下面列出了怎么用javax.persistence.IdClass的API类实例代码及写法,或者点击链接到github查看源代码。
public XClass getClassWithIdClass(boolean evenIfSubclass) {
if ( !evenIfSubclass && hasParents() ) {
return null;
}
if ( clazz.isAnnotationPresent( IdClass.class ) ) {
return clazz;
}
else {
InheritanceState state = InheritanceState.getSuperclassInheritanceState( clazz, inheritanceStatePerClass );
if ( state != null ) {
return state.getClassWithIdClass( true );
}
else {
return null;
}
}
}
public MybatisReflectionEntityInformation(Class<T> domainClass) {
super(domainClass);
IdClass idClass = domainClass.getAnnotation(IdClass.class);
if (null != idClass) {
this.idClass = idClass.value();
}
else {
for (Class<? extends Annotation> annotation : MybatisPersistentPropertyImpl.ID_ANNOTATIONS) {
AnnotationDetectionFieldCallback callback = new AnnotationDetectionFieldCallback(
annotation);
ReflectionUtils.doWithFields(domainClass, callback);
try {
this.field = callback.getRequiredField();
}
catch (IllegalStateException o_O) {
}
if (null != this.field) {
ReflectionUtils.makeAccessible(this.field);
break;
}
}
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Class<? extends Serializable> primaryKeyClass(Class<?> entityClass)
{
if (entityClass.isAnnotationPresent(IdClass.class))
{
return entityClass.getAnnotation(IdClass.class).value(); // Serializablity isn't required, could cause
// problems
}
Class clazz = PersistenceUnitDescriptorProvider.getInstance().primaryKeyIdClass(entityClass);
if (clazz != null)
{
return clazz;
}
Property<Serializable> property = primaryKeyProperty(entityClass);
return property.getJavaClass();
}
private static boolean isIdClassPkOfTheAssociatedEntity(
InheritanceState.ElementsToProcess elementsToProcess,
XClass compositeClass,
PropertyData inferredData,
PropertyData baseInferredData,
AccessType propertyAccessor,
Map<XClass, InheritanceState> inheritanceStatePerClass,
MetadataBuildingContext context) {
if ( elementsToProcess.getIdPropertyCount() == 1 ) {
final PropertyData idPropertyOnBaseClass = getUniqueIdPropertyFromBaseClass(
inferredData,
baseInferredData,
propertyAccessor,
context
);
final InheritanceState state = inheritanceStatePerClass.get( idPropertyOnBaseClass.getClassOrElement() );
if ( state == null ) {
return false; //while it is likely a user error, let's consider it is something that might happen
}
final XClass associatedClassWithIdClass = state.getClassWithIdClass( true );
if ( associatedClassWithIdClass == null ) {
//we cannot know for sure here unless we try and find the @EmbeddedId
//Let's not do this thorough checking but do some extra validation
final XProperty property = idPropertyOnBaseClass.getProperty();
return property.isAnnotationPresent( ManyToOne.class )
|| property.isAnnotationPresent( OneToOne.class );
}
else {
final XClass idClass = context.getBootstrapContext().getReflectionManager().toXClass(
associatedClassWithIdClass.getAnnotation( IdClass.class ).value()
);
return idClass.equals( compositeClass );
}
}
else {
return false;
}
}
private IdClass getIdClass(Element tree, XMLContext.Default defaults) {
Element element = tree == null ? null : tree.element( "id-class" );
if ( element != null ) {
Attribute attr = element.attribute( "class" );
if ( attr != null ) {
AnnotationDescriptor ad = new AnnotationDescriptor( IdClass.class );
Class clazz;
try {
clazz = classLoaderAccess.classForName( XMLContext.buildSafeClassName( attr.getValue(), defaults )
);
}
catch ( ClassLoadingException e ) {
throw new AnnotationException( "Unable to find id-class: " + attr.getValue(), e );
}
ad.setValue( "value", clazz );
return AnnotationFactory.create( ad );
}
else {
throw new AnnotationException( "id-class without class. " + SCHEMA_VALIDATION );
}
}
else if ( defaults.canUseJavaAnnotations() ) {
return getPhysicalAnnotation( IdClass.class );
}
else {
return null;
}
}
private boolean shouldOmitPropertyWithCompositeKey(Map<String, String> propertyAttributes)
{
// Extract simple type name of the relationship types
boolean isManyToOneRel = Boolean.parseBoolean(propertyAttributes.get("many-to-one"));
boolean isOneToOneRel = Boolean.parseBoolean(propertyAttributes.get("one-to-one"));
boolean isNToManyRel = Boolean.parseBoolean(propertyAttributes.get("n-to-many"));
if (isManyToOneRel || isNToManyRel || isOneToOneRel)
{
String rightHandSideType;
// Obtain the class name of the other/right-hand side of the relationship.
if (isOneToOneRel || isManyToOneRel)
{
rightHandSideType = propertyAttributes.get("type");
}
else
{
rightHandSideType = propertyAttributes.get("parameterized-type");
}
JavaClassSource javaClass = getJavaClass(rightHandSideType);
for (Member<?> member : javaClass.getMembers())
{
// FORGEPLUGINS-120 Ensure that properties with composite keys are detected and omitted from generation
if (member.hasAnnotation(Id.class) && !javaClass.hasAnnotation(IdClass.class))
{
return false;
}
if (member.hasAnnotation(EmbeddedId.class))
{
return true;
}
}
}
return false;
}
private static Class<?> fallbackIdTypeLookup(IdentifiableType<?> type) {
IdClass annotation = AnnotationUtils.findAnnotation(type.getJavaType(), IdClass.class);
return annotation == null ? null : annotation.value();
}