Example usage for org.hibernate.mapping ToOne isNullable

List of usage examples for org.hibernate.mapping ToOne isNullable

Introduction

In this page you can find the example usage for org.hibernate.mapping ToOne isNullable.

Prototype

public boolean isNullable() 

Source Link

Usage

From source file:org.wintersleep.codeviz.uml.model.HibernateModelFactory.java

License:Apache License

private void addRelations(CodeModel model) throws ClassNotFoundException {
    Iterator<PersistentClass> pci = configuration.getClassMappings();
    while (pci.hasNext()) {
        PersistentClass persistentClass = pci.next();
        final ModelClass mc = model.findModelClass(persistentClass.getMappedClass());
        Iterator<Property> pi = persistentClass.getPropertyIterator();
        while (pi.hasNext()) {
            Property property = pi.next();
            System.out.println(property);
            Value value = property.getValue();
            if (value instanceof ToOne) {
                ToOne toOne = (ToOne) value;
                ModelClass toClass = model.findModelClass(Class.forName(toOne.getReferencedEntityName()));
                RelationEndpoint fromEndpoint = new RelationEndpoint(mc, property.getName());
                fromEndpoint.setMinCardinality(toOne.isNullable() ? 0 : 1);
                RelationEndpoint toEndpoint = new RelationEndpoint(toClass, toOne.getReferencedPropertyName());
                toEndpoint.setMinCardinality(0);
                toEndpoint.setMaxCardinality(RelationEndpoint.MANY_CARDINALITY);
                if (value instanceof OneToOne) {
                    fromEndpoint.setMaxCardinality(1);
                } else if (value instanceof ManyToOne) {
                    fromEndpoint.setMaxCardinality(1);
                }//w w w  .  j  av  a2s  . c o m
                mc.addRelationTo(fromEndpoint, toEndpoint);
            } else if (value instanceof OneToMany) {
                OneToMany oneToMany = (OneToMany) value;
                oneToMany.getAssociatedClass();
                ModelClass toClass = model.findModelClass(oneToMany.getAssociatedClass().getMappedClass());
                RelationEndpoint fromEndpoint = new RelationEndpoint(mc, property.getName());
                fromEndpoint.setMinCardinality(0);
                fromEndpoint.setMaxCardinality(RelationEndpoint.MANY_CARDINALITY);
                RelationEndpoint toEndpoint = new RelationEndpoint(toClass, "TODO other");
                toEndpoint.setMinCardinality(oneToMany.isNullable() ? 0 : 1);
                toEndpoint.setMaxCardinality(1);
                mc.addRelationTo(fromEndpoint, toEndpoint);
            }
        }
    }
}