项目:ibankapp-base
文件:UniqueValidatorTest.java
@Test
@Transactional
public void testUniqueAndNum() {
thrown.expect(BasePersistenceException.class);
thrown.expectMessage("唯一约束校验失败,ENUM重复");
TestModelWithEumAndUnique model = new TestModelWithEumAndUnique();
model.setStatus(InheritanceType.SINGLE_TABLE);
model.setId("aaa");
repository.persist(model);
model = new TestModelWithEumAndUnique();
model.setId("bbb");
model.setStatus(InheritanceType.SINGLE_TABLE);
repository.persist(model);
}
项目:celerio
文件:InheritanceFactory.java
private void putEntityByTableNameForEntityWithInheritance() {
// Attention,for SINGLE_TABLE inheritance strategy,we only put the root entity.
for (EntityConfig entityConfig : config.getCelerio().getEntityConfigs()) {
Entity entity = config.getProject().getEntityByName(entityConfig.getEntityName());
if (entity.hasInheritance() && !config.getProject().hasEntityBySchemaAndTableName(entity.getTable().getSchemaName(),entity.getTable().getName())) {
InheritanceType inheritanceType = entity.getInheritance().getStrategy();
if (inheritanceType == InheritanceType.SINGLE_TABLE) {
if (entity.isRoot()) {
config.getProject().putEntity(entity);
}
} else if (inheritanceType == InheritanceType.JOINED || inheritanceType == InheritanceType.TABLE_PER_CLASS) {
config.getProject().putEntity(entity);
} else {
log.warning("Invalid case,there should be an inheritance type");
}
}
}
}
public void createEntity$Inheritance(Mapping context,ClassOutline classOutline,final Entity entity) {
final InheritanceType inheritanceStrategy = getInheritanceStrategy(
context,classOutline,entity);
if (isRootClass(context,classOutline)) {
if (entity.getInheritance() == null
|| entity.getInheritance().getStrategy() == null) {
entity.setInheritance(new Inheritance());
entity.getInheritance().setStrategy(inheritanceStrategy.name());
}
} else {
if (entity.getInheritance() != null
&& entity.getInheritance().getStrategy() != null) {
entity.setInheritance(null);
}
}
}
public javax.persistence.InheritanceType getInheritanceStrategy(
Mapping context,Entity entity) {
if (isRootClass(context,classOutline)) {
if (entity.getInheritance() != null
&& entity.getInheritance().getStrategy() != null) {
return InheritanceType.valueOf(entity.getInheritance()
.getStrategy());
} else {
return javax.persistence.InheritanceType.JOINED;
}
} else {
final ClassOutline superClassOutline = getSuperClass(context,classOutline);
final Entity superClassEntity = context.getCustomizing().getEntity(
superClassOutline);
return getInheritanceStrategy(context,superClassOutline,superClassEntity);
}
}
项目:org.fastnate
文件:EntityClass.java
/**
* Determine the inheritance type and discriminator properties.
*/
private void buildInheritance() {
// Check,if we've got an explicit inheritance type
final Inheritance inheritance = this.entityClass.getAnnotation(Inheritance.class);
if (inheritance != null) {
this.inheritanceType = inheritance.strategy();
}
// Find the root of our hierarchy
this.hierarchyRoot = this;
findHierarchyRoot(this.entityClass.getSuperclass());
// We scan only classes that we are about to write
// So we don't kNow,that there is a subclass entity - until we find one
// This Could be to late for InheritanceType.SINGLE_TABLE - the defaault type
// That's why we build a discriminator,if one of the inheritance annotations exists
if (this.inheritanceType == null && this.entityClass.isAnnotationPresent(discriminatorColumn.class)
|| this.entityClass.isAnnotationPresent(discriminatorValue.class)) {
this.inheritanceType = InheritanceType.SINGLE_TABLE;
}
builddiscriminator();
}
项目:lams
文件:InheritanceState.java
private void extractInheritanceType() {
XAnnotatedElement element = getClazz();
Inheritance inhAnn = element.getAnnotation( Inheritance.class );
MappedSuperclass mappedSuperClass = element.getAnnotation( MappedSuperclass.class );
if ( mappedSuperClass != null ) {
setEmbeddableSuperclass( true );
setType( inhAnn == null ? null : inhAnn.strategy() );
}
else {
setType( inhAnn == null ? InheritanceType.SINGLE_TABLE : inhAnn.strategy() );
}
}
项目:lams
文件:AnnotationBinder.java
/**
* For the mapped entities build some temporary data-structure containing @R_629_4045@ion about the
* inheritance status of a class.
*
* @param orderedClasses Order list of all annotated entities and their mapped superclasses
*
* @return A map of {@code InheritanceState}s keyed against their {@code XClass}.
*/
public static Map<XClass,InheritanceState> buildInheritanceStates(
List<XClass> orderedClasses,Mappings mappings) {
ReflectionManager reflectionManager = mappings.getReflectionManager();
Map<XClass,InheritanceState> inheritanceStatePerClass = new HashMap<XClass,InheritanceState>(
orderedClasses.size()
);
for ( XClass clazz : orderedClasses ) {
InheritanceState superclassstate = InheritanceState.getSuperclassInheritanceState(
clazz,inheritanceStatePerClass
);
InheritanceState state = new InheritanceState( clazz,inheritanceStatePerClass,mappings );
if ( superclassstate != null ) {
//the classes are ordered thus preventing an NPE
//FIXME if an entity has subclasses annotated @MappedSperclass wo sub @Entity this is wrong
superclassstate.setHasSiblings( true );
InheritanceState superEntityState = InheritanceState.getInheritanceStateOfSuperEntity(
clazz,inheritanceStatePerClass
);
state.setHasParents( superEntityState != null );
final boolean nonDefault = state.getType() != null && !InheritanceType.SINGLE_TABLE
.equals( state.getType() );
if ( superclassstate.getType() != null ) {
final boolean mixingStrategy = state.getType() != null && !state.getType()
.equals( superclassstate.getType() );
if ( nonDefault && mixingStrategy ) {
LOG.invalidSubStrategy( clazz.getName() );
}
state.setType( superclassstate.getType() );
}
}
inheritanceStatePerClass.put( clazz,state );
}
return inheritanceStatePerClass;
}
项目:lams
文件:JPAOverriddenAnnotationReader.java
private Inheritance getInheritance(Element tree,XMLContext.Default defaults) {
Element element = tree != null ? tree.element( "inheritance" ) : null;
if ( element != null ) {
AnnotationDescriptor ad = new AnnotationDescriptor( Inheritance.class );
Attribute attr = element.attribute( "strategy" );
InheritanceType strategy = InheritanceType.SINGLE_TABLE;
if ( attr != null ) {
String value = attr.getValue();
if ( "SINGLE_TABLE".equals( value ) ) {
strategy = InheritanceType.SINGLE_TABLE;
}
else if ( "JOINED".equals( value ) ) {
strategy = InheritanceType.JOINED;
}
else if ( "TABLE_PER_CLASS".equals( value ) ) {
strategy = InheritanceType.TABLE_PER_CLASS;
}
else {
throw new AnnotationException(
"UnkNown InheritanceType in XML: " + value + " (" + SCHEMA_VALIDATION + ")"
);
}
}
ad.setValue( "strategy",strategy );
return AnnotationFactory.create( ad );
}
else if ( defaults.canUseJavaAnnotations() ) {
return getPhysicalAnnotation( Inheritance.class );
}
else {
return null;
}
}
项目:jpa-unit
文件:EntityUtilsTest.java
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheClassNameWithSingleTableInheritance() throws Exception {
final String simpleClassNameBase = "EntityClass";
final String simpleClassNameA = "SubEntityClassA";
final String simpleClassNameB = "SubEntityClassB";
final JPackage jp = jcodemodel.rootPackage();
final JDefinedClass jbaseClass = jp._class(JMod.PUBLIC,simpleClassNameBase);
jbaseClass.annotate(Entity.class);
jbaseClass.annotate(Inheritance.class).param("strategy",InheritanceType.SINGLE_TABLE);
jbaseClass.annotate(discriminatorColumn.class).param("name","TYPE");
final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC,simpleClassNameA)._extends(jbaseClass);
jSubclassA.annotate(Entity.class);
jSubclassA.annotate(discriminatorValue.class).param("value","A");
final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC,simpleClassNameB)._extends(jbaseClass);
jSubclassB.annotate(Entity.class);
jSubclassB.annotate(discriminatorValue.class).param("value","B");
buildModel(testFolder.getRoot(),jcodemodel);
compileModel(testFolder.getRoot());
final Class<?> baseClass = loadClass(testFolder.getRoot(),jbaseClass.name());
final Class<?> subClassA = loadClass(testFolder.getRoot(),jSubclassA.name());
final Class<?> subClassB = loadClass(testFolder.getRoot(),jSubclassB.name());
final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(simpleClassNameBase),Arrays.asList(baseClass,subClassA,subClassB));
assertthat(clazz,equalTo(baseClass));
}
项目:jpa-unit
文件:EntityUtilsTest.java
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithSingleTableInheritance() throws Exception {
final String simpleClassNameBase = "EntityClass";
final String simpleClassNameA = "SubEntityClassA";
final String simpleClassNameB = "SubEntityClassB";
final String nodeLabel = "ENTITY_CLASS";
final JPackage jp = jcodemodel.rootPackage();
final JDefinedClass jbaseClass = jp._class(JMod.PUBLIC,simpleClassNameBase);
jbaseClass.annotate(Entity.class);
jbaseClass.annotate(Table.class).param("name",nodeLabel);
jbaseClass.annotate(Inheritance.class).param("strategy",jSubclassB.name());
final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(nodeLabel),equalTo(baseClass));
}
项目:jpa-unit
文件:EntityUtilsTest.java
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheClassNameWithTablePerClassInheritance() throws Exception {
final String simpleClassNameBase = "EntityClass";
final String simpleClassNameA = "SubEntityClassA";
final String simpleClassNameB = "SubEntityClassB";
final JPackage jp = jcodemodel.rootPackage();
final JDefinedClass jbaseClass = jp._class(JMod.PUBLIC,InheritanceType.TABLE_PER_CLASS);
final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC,simpleClassNameA)._extends(jbaseClass);
jSubclassA.annotate(Entity.class);
final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC,simpleClassNameB)._extends(jbaseClass);
jSubclassB.annotate(Entity.class);
buildModel(testFolder.getRoot(),jSubclassB.name());
final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(simpleClassNameB),equalTo(subClassB));
}
项目:jpa-unit
文件:EntityUtilsTest.java
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithTablePerClassInheritance() throws Exception {
final String simpleClassNameBase = "EntityClass";
final String simpleClassNameA = "SubEntityClassA";
final String simpleClassNameB = "SubEntityClassB";
final String nodeLabelBase = "ENTITY_CLASS";
final String nodeLabelA = "ENTITY_CLASS_A";
final String nodeLabelB = "ENTITY_CLASS_B";
final JPackage jp = jcodemodel.rootPackage();
final JDefinedClass jbaseClass = jp._class(JMod.PUBLIC,nodeLabelBase);
jbaseClass.annotate(Inheritance.class).param("strategy",simpleClassNameA)._extends(jbaseClass);
jSubclassA.annotate(Entity.class);
jSubclassA.annotate(Table.class).param("name",nodeLabelA);
final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC,simpleClassNameB)._extends(jbaseClass);
jSubclassB.annotate(Entity.class);
jSubclassB.annotate(Table.class).param("name",nodeLabelB);
buildModel(testFolder.getRoot(),jSubclassB.name());
final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(nodeLabelA),equalTo(subClassA));
}
项目:jpa-unit
文件:EntityUtilsTest.java
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheClassNameWithJoinedInheritance() throws Exception {
final String simpleClassNameBase = "EntityClass";
final String simpleClassNameA = "SubEntityClassA";
final String simpleClassNameB = "SubEntityClassB";
final JPackage jp = jcodemodel.rootPackage();
final JDefinedClass jbaseClass = jp._class(JMod.PUBLIC,InheritanceType.JOINED);
final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC,equalTo(subClassB));
}
项目:jpa-unit
文件:EntityUtilsTest.java
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithJoinedInheritance() throws Exception {
final String simpleClassNameBase = "EntityClass";
final String simpleClassNameA = "SubEntityClassA";
final String simpleClassNameB = "SubEntityClassB";
final String nodeLabelBase = "ENTITY_CLASS";
final String nodeLabelA = "ENTITY_CLASS_A";
final String nodeLabelB = "ENTITY_CLASS_B";
final JPackage jp = jcodemodel.rootPackage();
final JDefinedClass jbaseClass = jp._class(JMod.PUBLIC,equalTo(subClassA));
}
项目:jpa-unit
文件:EntityUtilsTest.java
@Test
public void testGetNamesOfIdPropertiesFromAClassHierarchyHavingAFieldAnnotatedWithId() throws Exception {
// GIVEN
final String simpleClassNameBase = "EntityClass";
final String simpleClassNameB = "SubEntityClass";
final String idPropertyName = "key";
final JPackage jp = jcodemodel.rootPackage();
final JDefinedClass jbaseClass = jp._class(JMod.PUBLIC,InheritanceType.TABLE_PER_CLASS);
jbaseClass.field(JMod.PRIVATE,String.class,idPropertyName).annotate(Id.class);
final JDefinedClass jSubclass = jp._class(JMod.PUBLIC,simpleClassNameB)._extends(jbaseClass);
jSubclass.annotate(Entity.class);
buildModel(testFolder.getRoot(),jcodemodel);
compileModel(testFolder.getRoot());
final Class<?> subClass = loadClass(testFolder.getRoot(),jSubclass.name());
// WHEN
final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(subClass);
// THEN
assertthat(namesOfIdProperties.size(),equalTo(1));
assertthat(namesOfIdProperties,hasItem(idPropertyName));
}
项目:jpa-unit
文件:EntityUtilsTest.java
@Test
public void testGetNamesOfIdPropertiesFromAClassHierarchyHavingAMethodAnnotatedWithId() throws Exception {
// GIVEN
final String simpleClassNameBase = "EntityClass";
final String simpleClassNameB = "SubEntityClass";
final String idPropertyName = "key";
final JPackage jp = jcodemodel.rootPackage();
final JDefinedClass jbaseClass = jp._class(JMod.PUBLIC,InheritanceType.TABLE_PER_CLASS);
jbaseClass.method(JMod.PUBLIC,jcodemodel.VOID,"getKey").annotate(Id.class);
final JDefinedClass jSubclass = jp._class(JMod.PUBLIC,hasItem(idPropertyName));
}
项目:jpa-unit
文件:EntityUtilsTest.java
@Test
public void testGetNamesOfIdPropertiesFromAClassHierarchyHavingAFieldAnnotatedWithEmbeddedId() throws Exception {
// GIVEN
final String simpleClassNameBase = "EntityClass";
final String simpleClassNameB = "SubEntityClass";
final String compositeIdPropertyName = "compositeKey";
final String id1PropertyName = "key1";
final String id2PropertyName = "key2";
final JPackage jp = jcodemodel.rootPackage();
final JDefinedClass jIdTypeClass = jp._class(JMod.PUBLIC,"IdType");
jIdTypeClass.annotate(Embeddable.class);
jIdTypeClass.field(JMod.PRIVATE,Integer.class,id1PropertyName);
jIdTypeClass.field(JMod.PRIVATE,id2PropertyName);
final JDefinedClass jbaseClass = jp._class(JMod.PUBLIC,jIdTypeClass,compositeIdPropertyName).annotate(EmbeddedId.class);
final JDefinedClass jSubclass = jp._class(JMod.PUBLIC,jcodemodel);
compileModel(testFolder.getRoot());
final Class<?> entityClass = loadClass(testFolder.getRoot(),jSubclass.name());
// WHEN
final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(entityClass);
// THEN
assertthat(namesOfIdProperties.size(),equalTo(2));
assertthat(namesOfIdProperties,hasItems(compositeIdPropertyName + "." + id1PropertyName,compositeIdPropertyName + "." + id2PropertyName));
}
项目:jpa-unit
文件:EntityUtilsTest.java
@Test
public void testGetNamesOfIdPropertiesFromAClassHierarchyHavingAMethodAnnotatedWithEmbeddedId() throws Exception {
// GIVEN
final String simpleClassNameBase = "EntityClass";
final String simpleClassNameB = "SubEntityClass";
final String compositeIdPropertyName = "compositeKey";
final String id1PropertyName = "key1";
final String id2PropertyName = "key2";
final JPackage jp = jcodemodel.rootPackage();
final JDefinedClass jIdTypeClass = jp._class(JMod.PUBLIC,InheritanceType.TABLE_PER_CLASS);
final JMethod method = jbaseClass.method(JMod.PUBLIC,"getCompositeKey");
method.annotate(EmbeddedId.class);
method.body()._return(JExpr._null());
final JDefinedClass jSubclass = jp._class(JMod.PUBLIC,compositeIdPropertyName + "." + id2PropertyName));
}
protected String getEntityNameForIdGeneration(MetaClass MetaClass) {
MetaClass result = MetaClass.getAncestors().stream()
.filter(mc -> {
// use root of inheritance tree if the strategy is JOINED because ID is stored in the root table
Class<?> javaClass = mc.getJavaClass();
Inheritance inheritance = javaClass.getAnnotation(Inheritance.class);
return inheritance != null && inheritance.strategy() == InheritanceType.JOINED;
})
.findFirst()
.orElse(MetaClass);
return result.getName();
}
private void resolveMissingInheritanceStrategyOnEntityConfigs(Map<String,EntityConfig> entityConfigsByEntityName) {
for (EntityConfig entityConfig : entityConfigsByEntityName.values()) {
if (!entityConfig.hasInheritance()) {
continue;
}
EntityConfig current = entityConfig;
while (current.hasParentEntityName()) {
current = entityConfigsByEntityName.get(current.getParentEntityName().toupperCase());
Assert.notNull(current,"The parent entity " + current.getParentEntityName() + " Could not be found in the configuration.");
}
// root may use default...
if (!current.getInheritance().hasstrategy()) {
// default...
current.getInheritance().setStrategy(InheritanceType.SINGLE_TABLE);
}
if (entityConfig.getInheritance().hasstrategy()) {
Assert.isTrue(
entityConfig.getInheritance().getStrategy() == current.getInheritance().getStrategy(),"The entityConfig " + entityConfig.getEntityName()
+ " must not declare an inheritance strategy that is different from the strategy declared in the root entity "
+ current.getEntityName());
}
// for internal convenient purposes we propagate it
entityConfig.getInheritance().setStrategy(current.getInheritance().getStrategy());
}
}
项目:celerio
文件:Entity.java
public boolean is(InheritanceType strategy) {
Assert.notNull(strategy);
if (getInheritance() == null) {
return false;
}
return strategy == getInheritance().getStrategy();
}
项目:celerio
文件:Inheritance.java
public boolean is(InheritanceType strategy) {
return this.strategy == strategy;
}
private void createEntity$Table(Mapping context,Entity entity) {
final InheritanceType inheritanceStrategy = getInheritanceStrategy(
context,entity);
switch (inheritanceStrategy) {
case JOINED:
if (entity.getTable() == null) {
entity.setTable(new Table());
}
createTable(context,entity.getTable());
break;
case SINGLE_TABLE:
if (isRootClass(context,classOutline)) {
if (entity.getTable() == null) {
entity.setTable(new Table());
}
createTable(context,entity.getTable());
} else {
if (entity.getTable() != null) {
entity.setTable(null);
}
}
break;
case TABLE_PER_CLASS:
if (entity.getTable() == null) {
entity.setTable(new Table());
}
createTable(context,entity.getTable());
break;
default:
throw new IllegalArgumentException("UnkNown inheritance strategy.");
}
}
项目:org.fastnate
文件:EntityClass.java
private void builddiscriminator() {
if (this.inheritanceType == InheritanceType.SINGLE_TABLE || this.inheritanceType == InheritanceType.JOINED) {
final discriminatorColumn column = this.hierarchyRoot.entityClass.getAnnotation(discriminatorColumn.class);
if (column != null || this.inheritanceType != InheritanceType.JOINED
|| this.context.getProvider().isJoineddiscriminatorNeeded()) {
this.discriminatorColumn = this.table.resolveColumn(column == null ? "DTYPE" : column.name());
this.discriminator = builddiscriminator(this,column);
}
}
}
项目:org.fastnate
文件:EntityClass.java
private void findHierarchyRoot(final Class<? super E> inspectedClass) {
if (inspectedClass != null) {
if (!inspectedClass.isAnnotationPresent(Entity.class)) {
findHierarchyRoot(inspectedClass.getSuperclass());
} else {
this.parentEntityClass = inspectedClass;
final EntityClass<? super E> parentDescription = this.context.getDescription(inspectedClass);
this.accessstyle = parentDescription.getAccessstyle();
if (parentDescription.inheritanceType == null) {
parentDescription.inheritanceType = InheritanceType.SINGLE_TABLE;
parentDescription.builddiscriminator();
}
if (this.inheritanceType == null) {
this.inheritanceType = parentDescription.inheritanceType;
this.hierarchyRoot = parentDescription.hierarchyRoot;
} else if (parentDescription.inheritanceType != InheritanceType.TABLE_PER_CLASS) {
this.hierarchyRoot = parentDescription.hierarchyRoot;
}
if (parentDescription.getInheritanceType() == InheritanceType.JOINED) {
this.joinedParentClass = parentDescription;
buildPrimaryKeyJoinColumn();
} else {
if (parentDescription.getInheritanceType() == InheritanceType.SINGLE_TABLE) {
this.table = parentDescription.table;
}
this.joinedParentClass = parentDescription.joinedParentClass;
this.primaryKeyJoinColumn = parentDescription.primaryKeyJoinColumn;
}
}
}
}
项目:lams
文件:InheritanceState.java
boolean hasTable() {
return !hasParents() || !InheritanceType.SINGLE_TABLE.equals( getType() );
}
项目:lams
文件:InheritanceState.java
boolean hasDenormalizedTable() {
return hasParents() && InheritanceType.TABLE_PER_CLASS.equals( getType() );
}
项目:lams
文件:InheritanceState.java
public InheritanceType getType() {
return type;
}
项目:lams
文件:InheritanceState.java
public void setType(InheritanceType type) {
this.type = type;
}
项目:ibankapp-base
文件:TestModelWithEumAndUnique.java
@Column
@Enumerated(EnumType.STRING)
public InheritanceType getStatus() {
return status;
}
项目:ibankapp-base
文件:TestModelWithEumAndUnique.java
void setStatus(InheritanceType status) {
this.status = status;
}
public boolean is(InheritanceType strategy) {
return hasInheritance() && getInheritance().is(strategy);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。