use of javax.persistence.Id in project hibernate-orm by hibernate.
the class JPAOverriddenAnnotationReaderTest method testIdRelatedAnnotations.
@Test
public void testIdRelatedAnnotations() throws Exception {
XMLContext context = buildContext("org/hibernate/test/annotations/reflection/orm.xml");
Method method = Administration.class.getDeclaredMethod("getId");
JPAOverriddenAnnotationReader reader = new JPAOverriddenAnnotationReader(method, context, ClassLoaderAccessTestingImpl.INSTANCE);
assertNull(reader.getAnnotation(Id.class));
assertNull(reader.getAnnotation(Column.class));
Field field = Administration.class.getDeclaredField("id");
reader = new JPAOverriddenAnnotationReader(field, context, ClassLoaderAccessTestingImpl.INSTANCE);
assertNotNull(reader.getAnnotation(Id.class));
assertNotNull(reader.getAnnotation(GeneratedValue.class));
assertEquals(GenerationType.SEQUENCE, reader.getAnnotation(GeneratedValue.class).strategy());
assertEquals("generator", reader.getAnnotation(GeneratedValue.class).generator());
assertNotNull(reader.getAnnotation(SequenceGenerator.class));
assertEquals("seq", reader.getAnnotation(SequenceGenerator.class).sequenceName());
assertNotNull(reader.getAnnotation(Columns.class));
assertEquals(1, reader.getAnnotation(Columns.class).columns().length);
assertEquals("fld_id", reader.getAnnotation(Columns.class).columns()[0].name());
assertNotNull(reader.getAnnotation(Temporal.class));
assertEquals(TemporalType.DATE, reader.getAnnotation(Temporal.class).value());
context = buildContext("org/hibernate/test/annotations/reflection/metadata-complete.xml");
method = Administration.class.getDeclaredMethod("getId");
reader = new JPAOverriddenAnnotationReader(method, context, ClassLoaderAccessTestingImpl.INSTANCE);
assertNotNull("Default access type when not defined in metadata complete should be property", reader.getAnnotation(Id.class));
field = Administration.class.getDeclaredField("id");
reader = new JPAOverriddenAnnotationReader(field, context, ClassLoaderAccessTestingImpl.INSTANCE);
assertNull("Default access type when not defined in metadata complete should be property", reader.getAnnotation(Id.class));
method = BusTrip.class.getDeclaredMethod("getId");
reader = new JPAOverriddenAnnotationReader(method, context, ClassLoaderAccessTestingImpl.INSTANCE);
assertNull(reader.getAnnotation(EmbeddedId.class));
field = BusTrip.class.getDeclaredField("id");
reader = new JPAOverriddenAnnotationReader(field, context, ClassLoaderAccessTestingImpl.INSTANCE);
assertNotNull(reader.getAnnotation(EmbeddedId.class));
assertNotNull(reader.getAnnotation(AttributeOverrides.class));
assertEquals(1, reader.getAnnotation(AttributeOverrides.class).value().length);
}
use of javax.persistence.Id in project hibernate-orm by hibernate.
the class AnnotationBinder method addProperty.
private static int addProperty(PropertyContainer propertyContainer, XProperty property, List<PropertyData> inFlightPropertyDataList, MetadataBuildingContext context) {
// and if so, skip it..
for (PropertyData propertyData : inFlightPropertyDataList) {
if (propertyData.getPropertyName().equals(property.getName())) {
// EARLY EXIT!!!
return 0;
}
}
final XClass declaringClass = propertyContainer.getDeclaringClass();
final XClass entity = propertyContainer.getEntityAtStake();
int idPropertyCounter = 0;
PropertyData propertyAnnotatedElement = new PropertyInferredData(declaringClass, property, propertyContainer.getClassLevelAccessType().getType(), context.getBuildingOptions().getReflectionManager());
/*
* put element annotated by @Id in front
* since it has to be parsed beforeQuery any association by Hibernate
*/
final XAnnotatedElement element = propertyAnnotatedElement.getProperty();
if (element.isAnnotationPresent(Id.class) || element.isAnnotationPresent(EmbeddedId.class)) {
inFlightPropertyDataList.add(0, propertyAnnotatedElement);
/**
* The property must be put in hibernate.properties as it's a system wide property. Fixable?
* TODO support true/false/default on the property instead of present / not present
* TODO is @Column mandatory?
* TODO add method support
*/
if (context.getBuildingOptions().isSpecjProprietarySyntaxEnabled()) {
if (element.isAnnotationPresent(Id.class) && element.isAnnotationPresent(Column.class)) {
String columnName = element.getAnnotation(Column.class).name();
for (XProperty prop : declaringClass.getDeclaredProperties(AccessType.FIELD.getType())) {
if (!prop.isAnnotationPresent(MapsId.class)) {
/**
* The detection of a configured individual JoinColumn differs between Annotation
* and XML configuration processing.
*/
boolean isRequiredAnnotationPresent = false;
JoinColumns groupAnnotation = prop.getAnnotation(JoinColumns.class);
if ((prop.isAnnotationPresent(JoinColumn.class) && prop.getAnnotation(JoinColumn.class).name().equals(columnName))) {
isRequiredAnnotationPresent = true;
} else if (prop.isAnnotationPresent(JoinColumns.class)) {
for (JoinColumn columnAnnotation : groupAnnotation.value()) {
if (columnName.equals(columnAnnotation.name())) {
isRequiredAnnotationPresent = true;
break;
}
}
}
if (isRequiredAnnotationPresent) {
//create a PropertyData fpr the specJ property holding the mapping
PropertyData specJPropertyData = new PropertyInferredData(declaringClass, //same dec
prop, // the actual @XToOne property
propertyContainer.getClassLevelAccessType().getType(), //TODO we should get the right accessor but the same as id would do
context.getBuildingOptions().getReflectionManager());
context.getMetadataCollector().addPropertyAnnotatedWithMapsIdSpecj(entity, specJPropertyData, element.toString());
}
}
}
}
}
if (element.isAnnotationPresent(ManyToOne.class) || element.isAnnotationPresent(OneToOne.class)) {
context.getMetadataCollector().addToOneAndIdProperty(entity, propertyAnnotatedElement);
}
idPropertyCounter++;
} else {
inFlightPropertyDataList.add(propertyAnnotatedElement);
}
if (element.isAnnotationPresent(MapsId.class)) {
context.getMetadataCollector().addPropertyAnnotatedWithMapsId(entity, propertyAnnotatedElement);
}
return idPropertyCounter;
}
use of javax.persistence.Id in project eweb4j-framework by laiweiwei.
the class PojoAnnotationConfig method getProperties.
private static List<Property> getProperties(Class<?> clazz, final List<Property> pList, final boolean requireSuper, Log log) throws Throwable {
List<Property> result = new ArrayList<Property>();
ReflectUtil ru;
try {
ru = new ReflectUtil(clazz);
ru.setRequiredSuper(requireSuper);
} catch (Throwable e) {
log.warn(e.toString(), e);
throw e;
}
for (Field f : ru.getFields()) {
if (Collection.class.isAssignableFrom(f.getType()))
continue;
String name = f.getName();
Method getter = ru.getGetter(name);
if (getter == null)
continue;
Ignore igAnn = f.getAnnotation(Ignore.class);
if (igAnn == null)
igAnn = getter.getAnnotation(Ignore.class);
if (igAnn != null)
continue;
Transient trans = f.getAnnotation(Transient.class);
if (trans == null)
trans = getter.getAnnotation(Transient.class);
if (trans != null)
continue;
OneToMany manyAnn = getter.getAnnotation(OneToMany.class);
if (manyAnn != null)
continue;
else {
manyAnn = f.getAnnotation(OneToMany.class);
if (manyAnn != null)
continue;
}
ManyToMany manyManyAnn = getter.getAnnotation(ManyToMany.class);
if (manyManyAnn != null)
continue;
else {
manyManyAnn = f.getAnnotation(ManyToMany.class);
if (manyManyAnn != null)
continue;
}
Property p = new Property();
if (Long.class.isAssignableFrom(f.getType()) || long.class.isAssignableFrom(f.getType()))
p.setSize("20");
else if (Integer.class.isAssignableFrom(f.getType()) || int.class.isAssignableFrom(f.getType()))
p.setSize("8");
else if (String.class.isAssignableFrom(f.getType()))
p.setSize("255");
else if (Boolean.class.isAssignableFrom(f.getType()) || boolean.class.isAssignableFrom(f.getType()))
p.setSize("");
else if (Float.class.isAssignableFrom(f.getType()) || float.class.isAssignableFrom(f.getType()))
p.setSize("8");
Id idAnn = getter.getAnnotation(Id.class);
if (idAnn == null)
idAnn = f.getAnnotation(Id.class);
if (idAnn != null) {
if (pList != null && hasIdProperty(pList))
continue;
p.setAutoIncrement("1");
p.setPk("1");
p.setSize("20");
}
Column colAnn = getter.getAnnotation(Column.class);
if (colAnn == null) {
colAnn = f.getAnnotation(Column.class);
}
String column = colAnn == null ? "" : colAnn.name();
column = "".equals(column.trim()) ? name : column;
p.setName(name);
p.setColumn(column);
p.setType(f.getType().getName());
p.setNotNull("false");
if (colAnn != null) {
// int size = colAnn.length();
p.setNotNull(String.valueOf(!colAnn.nullable()));
p.setUnique(String.valueOf(colAnn.unique()));
}
if (ClassUtil.isPojo(f.getType())) {
OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
if (oneAnn == null)
oneAnn = f.getAnnotation(OneToOne.class);
ManyToOne manyToOneAnn = null;
if (oneAnn == null) {
manyToOneAnn = getter.getAnnotation(ManyToOne.class);
if (manyToOneAnn == null)
manyToOneAnn = f.getAnnotation(ManyToOne.class);
}
if (oneAnn != null || manyToOneAnn != null) {
if (oneAnn != null)
p.setType(PropType.ONE_ONE);
else
p.setType(PropType.MANY_ONE);
JoinColumn joinColumn = getter.getAnnotation(JoinColumn.class);
if (joinColumn == null)
joinColumn = f.getAnnotation(JoinColumn.class);
if (joinColumn != null && joinColumn.name().trim().length() > 0)
p.setColumn(joinColumn.name());
else
p.setColumn(f.getName() + "_id");
p.setRelProperty(null);
String refCol = null;
if (joinColumn != null && joinColumn.referencedColumnName().trim().length() > 0) {
refCol = joinColumn.referencedColumnName();
if (refCol != null && refCol.trim().length() > 0) {
String relField = ORMConfigBeanUtil.getField(f.getType(), refCol);
if (relField != null && relField.trim().length() > 0)
p.setRelProperty(relField);
}
}
p.setRelClass(f.getType());
p.setSize("20");
}
}
result.add(p);
}
return result;
}
use of javax.persistence.Id in project CloudStack-archive by CloudStack-extras.
the class Attribute method setupColumnInfo.
protected void setupColumnInfo(Class<?> clazz, AttributeOverride[] overrides, String tableName, boolean isEmbedded, boolean isId) {
flags = Flag.Selectable.setTrue(flags);
GeneratedValue gv = field.getAnnotation(GeneratedValue.class);
if (gv != null) {
if (gv.strategy() == GenerationType.IDENTITY) {
flags = Flag.DbGenerated.setTrue(flags);
} else if (gv.strategy() == GenerationType.SEQUENCE) {
assert (false) : "Sequence generation not supported.";
flags = Flag.DaoGenerated.setTrue(flags);
flags = Flag.Insertable.setTrue(flags);
flags = Flag.SequenceGV.setTrue(flags);
} else if (gv.strategy() == GenerationType.TABLE) {
flags = Flag.DaoGenerated.setTrue(flags);
flags = Flag.Insertable.setTrue(flags);
flags = Flag.TableGV.setTrue(flags);
} else if (gv.strategy() == GenerationType.AUTO) {
flags = Flag.DaoGenerated.setTrue(flags);
flags = Flag.Insertable.setTrue(flags);
flags = Flag.AutoGV.setTrue(flags);
}
}
if (isEmbedded) {
flags = Flag.Embedded.setTrue(flags);
}
if (isId) {
flags = Flag.Id.setTrue(flags);
} else {
Id id = field.getAnnotation(Id.class);
if (id != null) {
flags = Flag.Id.setTrue(flags);
}
}
column = field.getAnnotation(Column.class);
if (gv == null) {
if (column == null || (column.insertable() && column.table().length() == 0)) {
flags = Flag.Insertable.setTrue(flags);
}
if (column == null || (column.updatable() && column.table().length() == 0)) {
flags = Flag.Updatable.setTrue(flags);
}
if (column == null || column.nullable()) {
flags = Flag.Nullable.setTrue(flags);
}
if (column != null && column.encryptable()) {
flags = Flag.Encrypted.setTrue(flags);
}
}
ElementCollection ec = field.getAnnotation(ElementCollection.class);
if (ec != null) {
flags = Flag.Insertable.setFalse(flags);
flags = Flag.Selectable.setFalse(flags);
}
Temporal temporal = field.getAnnotation(Temporal.class);
if (temporal != null) {
if (temporal.value() == TemporalType.DATE) {
flags = Flag.Date.setTrue(flags);
} else if (temporal.value() == TemporalType.TIME) {
flags = Flag.Time.setTrue(flags);
} else if (temporal.value() == TemporalType.TIMESTAMP) {
flags = Flag.TimeStamp.setTrue(flags);
}
}
if (column != null && column.table().length() > 0) {
table = column.table();
}
columnName = DbUtil.getColumnName(field, overrides);
}
use of javax.persistence.Id in project Activiti by Activiti.
the class JPAEntityScanner method getIdField.
private Field getIdField(Class<?> clazz) {
Field idField = null;
Field[] fields = clazz.getDeclaredFields();
Id idAnnotation = null;
for (Field field : fields) {
idAnnotation = field.getAnnotation(Id.class);
if (idAnnotation != null) {
idField = field;
break;
}
}
if (idField == null) {
// Check superClass for fields with @Id, since getDeclaredFields does
// not return superclass-fields.
Class<?> superClass = clazz.getSuperclass();
if (superClass != null && !superClass.equals(Object.class)) {
// Recursively go up class hierarchy
idField = getIdField(superClass);
}
}
return idField;
}
Aggregations