下面列出了怎么用javax.persistence.OneToOne的API类实例代码及写法,或者点击链接到github查看源代码。
private boolean getCascade(ManyToMany manyManyAnnotation, ManyToOne manyOneAnnotation, OneToMany oneManyAnnotation,
OneToOne oneOneAnnotation) {
if (manyManyAnnotation != null) {
return getCascade(manyManyAnnotation.cascade());
}
if (manyOneAnnotation != null) {
return getCascade(manyOneAnnotation.cascade());
}
if (oneManyAnnotation != null) {
return getCascade(oneManyAnnotation.cascade());
}
if (oneOneAnnotation != null) {
return getCascade(oneOneAnnotation.cascade());
}
return false;
}
@Override
public Optional<ResourceFieldType> getFieldType(BeanAttributeInformation attributeDesc) {
Optional<OneToOne> oneToOne = attributeDesc.getAnnotation(OneToOne.class);
Optional<OneToMany> oneToMany = attributeDesc.getAnnotation(OneToMany.class);
Optional<ManyToOne> manyToOne = attributeDesc.getAnnotation(ManyToOne.class);
Optional<ManyToMany> manyToMany = attributeDesc.getAnnotation(ManyToMany.class);
if (oneToOne.isPresent() || oneToMany.isPresent() || manyToOne.isPresent() || manyToMany.isPresent()) {
return Optional.of(ResourceFieldType.RELATIONSHIP);
}
Optional<Id> id = attributeDesc.getAnnotation(Id.class);
Optional<EmbeddedId> embeddedId = attributeDesc.getAnnotation(EmbeddedId.class);
if (id.isPresent() || embeddedId.isPresent()) {
return Optional.of(ResourceFieldType.ID);
}
return Optional.empty();
}
private static boolean hasAnnotationsOnIdClass(XClass idClass) {
// if(idClass.getAnnotation(Embeddable.class) != null)
// return true;
List<XProperty> properties = idClass.getDeclaredProperties( XClass.ACCESS_FIELD );
for ( XProperty property : properties ) {
if ( property.isAnnotationPresent( Column.class ) || property.isAnnotationPresent( OneToMany.class ) ||
property.isAnnotationPresent( ManyToOne.class ) || property.isAnnotationPresent( Id.class ) ||
property.isAnnotationPresent( GeneratedValue.class ) || property.isAnnotationPresent( OneToOne.class ) ||
property.isAnnotationPresent( ManyToMany.class )
) {
return true;
}
}
List<XMethod> methods = idClass.getDeclaredMethods();
for ( XMethod method : methods ) {
if ( method.isAnnotationPresent( Column.class ) || method.isAnnotationPresent( OneToMany.class ) ||
method.isAnnotationPresent( ManyToOne.class ) || method.isAnnotationPresent( Id.class ) ||
method.isAnnotationPresent( GeneratedValue.class ) || method.isAnnotationPresent( OneToOne.class ) ||
method.isAnnotationPresent( ManyToMany.class )
) {
return true;
}
}
return false;
}
private static String getMappedByFromAnnotation(CtField persistentField) {
OneToOne oto = PersistentAttributesHelper.getAnnotation( persistentField, OneToOne.class );
if ( oto != null ) {
return oto.mappedBy();
}
OneToMany otm = PersistentAttributesHelper.getAnnotation( persistentField, OneToMany.class );
if ( otm != null ) {
return otm.mappedBy();
}
// For @ManyToOne associations, mappedBy must come from the @OneToMany side of the association
ManyToMany mtm = PersistentAttributesHelper.getAnnotation( persistentField, ManyToMany.class );
return mtm == null ? "" : mtm.mappedBy();
}
private static String getMappedByNotManyToMany(FieldDescription target) {
try {
AnnotationDescription.Loadable<OneToOne> oto = EnhancerImpl.getAnnotation( target, OneToOne.class );
if ( oto != null ) {
return oto.getValue( new MethodDescription.ForLoadedMethod( OneToOne.class.getDeclaredMethod( "mappedBy" ) ) ).resolve( String.class );
}
AnnotationDescription.Loadable<OneToMany> otm = EnhancerImpl.getAnnotation( target, OneToMany.class );
if ( otm != null ) {
return otm.getValue( new MethodDescription.ForLoadedMethod( OneToMany.class.getDeclaredMethod( "mappedBy" ) ) ).resolve( String.class );
}
AnnotationDescription.Loadable<ManyToMany> mtm = EnhancerImpl.getAnnotation( target, ManyToMany.class );
if ( mtm != null ) {
return mtm.getValue( new MethodDescription.ForLoadedMethod( ManyToMany.class.getDeclaredMethod( "mappedBy" ) ) ).resolve( String.class );
}
}
catch (NoSuchMethodException ignored) {
}
return null;
}
@Override
protected Expression processOneToOneMapping(OneToOneMapping mapping) {
ClassDescriptor descriptor = mapping.getDescriptor();
Field referenceField = FieldUtils.getAllFieldsList(descriptor.getJavaClass())
.stream().filter(f -> f.getName().equals(mapping.getAttributeName()))
.findFirst().orElse(null);
if (SoftDelete.class.isAssignableFrom(mapping.getReferenceClass()) && referenceField != null) {
OneToOne oneToOne = referenceField.getAnnotation(OneToOne.class);
if (oneToOne != null && !Strings.isNullOrEmpty(oneToOne.mappedBy())) {
return new ExpressionBuilder().get("deleteTs").isNull();
}
}
return null;
}
@Cascade(CascadeType.ALL)
@OneToOne(fetch = FetchType.LAZY)
// disable foreign key, to be able to remove runtime data
@JoinColumn(name = "CLEAN_SCRIPT_ID", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
public ScriptData getCleanScript() {
return cleanScript;
}
/**
* Gets the mappedBy value from an attribute
* @param attribute attribute
* @return mappedBy value or null if none.
*/
public static String getMappedBy(Attribute<?, ?> attribute) {
String mappedBy = null;
if (attribute.isAssociation()) {
Annotation[] annotations = null;
Member member = attribute.getJavaMember();
if (member instanceof Field) {
annotations = ((Field) member).getAnnotations();
}
else if (member instanceof Method) {
annotations = ((Method) member).getAnnotations();
}
for (Annotation a : annotations) {
if (a.annotationType().equals(OneToMany.class)) {
mappedBy = ((OneToMany) a).mappedBy();
break;
}
else if (a.annotationType().equals(ManyToMany.class)) {
mappedBy = ((ManyToMany) a).mappedBy();
break;
}
else if (a.annotationType().equals(OneToOne.class)) {
mappedBy = ((OneToOne) a).mappedBy();
break;
}
}
}
return "".equals(mappedBy) ? null : mappedBy;
}
@Cascade(org.hibernate.annotations.CascadeType.ALL)
@OneToOne(fetch = FetchType.LAZY)
// disable foreign key, to be able to remove runtime data
@JoinColumn(name = "ENV_SCRIPT_ID", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
public ScriptData getEnvScript() {
return envScript;
}
@Override
protected RelationshipRepositoryBehavior getDefaultRelationshipRepositoryBehavior(BeanAttributeInformation attributeDesc) {
Optional<OneToOne> oneToOne = attributeDesc.getAnnotation(OneToOne.class);
Optional<OneToMany> oneToMany = attributeDesc.getAnnotation(OneToMany.class);
Optional<ManyToOne> manyToOne = attributeDesc.getAnnotation(ManyToOne.class);
Optional<ManyToMany> manyToMany = attributeDesc.getAnnotation(ManyToMany.class);
if (oneToOne.isPresent() || manyToOne.isPresent() || oneToMany.isPresent() || manyToMany.isPresent()) {
Optional<String> mappedBy = getMappedBy(attributeDesc);
if (mappedBy.isPresent() && mappedBy.get().length() > 0) {
return RelationshipRepositoryBehavior.FORWARD_OPPOSITE;
}
return RelationshipRepositoryBehavior.FORWARD_OWNER;
}
return RelationshipRepositoryBehavior.DEFAULT;
}
@Cascade(CascadeType.ALL)
@OneToOne(fetch = FetchType.LAZY)
// disable foreign key, to be able to remove runtime data
@JoinColumn(name = "FLOW_SCRIPT_ID", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
public ScriptData getFlowScript() {
return flowScript;
}
Ejb3JoinColumn[] buildDefaultJoinColumnsForXToOne(XProperty property, PropertyData inferredData) {
Ejb3JoinColumn[] joinColumns;
JoinTable joinTableAnn = propertyHolder.getJoinTable( property );
if ( joinTableAnn != null ) {
joinColumns = Ejb3JoinColumn.buildJoinColumns(
joinTableAnn.inverseJoinColumns(),
null,
entityBinder.getSecondaryTables(),
propertyHolder,
inferredData.getPropertyName(),
buildingContext
);
if ( StringHelper.isEmpty( joinTableAnn.name() ) ) {
throw new AnnotationException(
"JoinTable.name() on a @ToOne association has to be explicit: "
+ BinderHelper.getPath( propertyHolder, inferredData )
);
}
}
else {
OneToOne oneToOneAnn = property.getAnnotation( OneToOne.class );
String mappedBy = oneToOneAnn != null
? oneToOneAnn.mappedBy()
: null;
joinColumns = Ejb3JoinColumn.buildJoinColumns(
null,
mappedBy,
entityBinder.getSecondaryTables(),
propertyHolder,
inferredData.getPropertyName(),
buildingContext
);
}
return joinColumns;
}
private static Class<?> getTargetEntityClass(XProperty property) {
final ManyToOne mTo = property.getAnnotation( ManyToOne.class );
if (mTo != null) {
return mTo.targetEntity();
}
final OneToOne oTo = property.getAnnotation( OneToOne.class );
if (oTo != null) {
return oTo.targetEntity();
}
throw new AssertionFailure("Unexpected discovery of a targetEntity: " + property.getName() );
}
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;
}
}
public static CtClass getTargetEntityClass(CtClass managedCtClass, CtField persistentField) throws NotFoundException {
// get targetEntity defined in the annotation
try {
OneToOne oto = PersistentAttributesHelper.getAnnotation( persistentField, OneToOne.class );
OneToMany otm = PersistentAttributesHelper.getAnnotation( persistentField, OneToMany.class );
ManyToOne mto = PersistentAttributesHelper.getAnnotation( persistentField, ManyToOne.class );
ManyToMany mtm = PersistentAttributesHelper.getAnnotation( persistentField, ManyToMany.class );
Class<?> targetClass = null;
if ( oto != null ) {
targetClass = oto.targetEntity();
}
if ( otm != null ) {
targetClass = otm.targetEntity();
}
if ( mto != null ) {
targetClass = mto.targetEntity();
}
if ( mtm != null ) {
targetClass = mtm.targetEntity();
}
if ( targetClass != null && targetClass != void.class ) {
return managedCtClass.getClassPool().get( targetClass.getName() );
}
}
catch (NotFoundException ignore) {
}
// infer targetEntity from generic type signature
String inferredTypeName = inferTypeName( managedCtClass, persistentField.getName() );
return inferredTypeName == null ? null : managedCtClass.getClassPool().get( inferredTypeName );
}
protected void addAssertValidCheckIfRequired(final Annotation constraintAnnotation, final Collection<Check> checks,
@SuppressWarnings("unused") /*parameter for potential use by subclasses*/final AccessibleObject fieldOrMethod) {
if (containsCheckOfType(checks, AssertValidCheck.class))
return;
if (constraintAnnotation instanceof OneToOne || constraintAnnotation instanceof OneToMany || constraintAnnotation instanceof ManyToOne
|| constraintAnnotation instanceof ManyToMany) {
checks.add(new AssertValidCheck());
}
}
@OneToOne(mappedBy = "childIntegrateTemplateAlias")
public AdmIntegrateTemplateRelation getParentIntegrateTemplateRelation() {
return this.parentIntegrateTemplateRelation;
}
@OneToOne
@JoinColumn(name="creater",insertable=false , updatable = false,unique=false)
public User getUser() {
return user;
}
@OneToOne
public User getUser() {
return user;
}
@OneToOne(optional = true)
public Organ getOrgan() {
return organ;
}
@OneToOne
public IMGroup getImgroup() {
return imgroup;
}
@Override
@OneToOne(targetEntity = BusinessValueList.class, cascade = CascadeType.ALL)
@JoinColumn(name = "business_value_id")
public IBusinessValueList getBusinessValueList() {
return businessValueList;
}
@OneToOne(optional = true)
public Role getRole() {
return role;
}
@OneToOne(fetch = FetchType.LAZY, mappedBy = "resource")
public ImageText getImageText() {
return this.imageText;
}
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "creator_user_id", updatable = false)
public SysUser getCreator() {
return creator;
}
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "modifier_user_id")
public SysUser getModifier() {
return modifier;
}
@OneToOne(fetch = FetchType.LAZY, mappedBy = "resource")
public Link getLink() {
return this.link;
}
/**
* @return Referenced entity.
*/
@OneToOne
public Entity getEntity() {
return entity;
}
@OneToOne(mappedBy = "holder")
public Passport getPassport() {
return passport;
}
@BeforeMethod
public void setup() {
handler = new OneToOneAnnotationHandler();
annotation = mock(OneToOne.class);
metaData = mock(EntityMetaData.class);
}