spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

How to keep a 1 to 1 relation consistent


Finally, here's your solution:

public class A {
    private B b;

    public void setB(B b) {
        if(this.b != null) {
            this.b.unsetA();
        }
        this.b = b;
        if(b != null && b.getA() != this)
{
            this.b.setA(this);
        }
    }

    public void unsetB() {
        this.b = null;
    }

    public B getB() {
        return b;
    }

}

public class B {
    private A a;

    public void setA(A a) {
        if(this.a != null) {
            this.a.unsetB();
        }
        this.a = a;
        if(a != null && a.getB() != this)
{
            this.a.setB(this);
        }
    }

    public void unsetA() {
        this.a = null;
    }

    public A getA() {
        return a;
    }

}

My Test Class:

public class Test {

    public static void main(String ... args) {
        A a1 = new A();
        B b1 = new B();
        A a2 = new A();
        B b2 = new B();

        checkForZeroOrTwoRelations(a1, a2, b1,
b2);
        a1.setB(b1);
        checkForZeroOrTwoRelations(a1, a2, b1,
b2);     
        b1.setA(a2);
        checkForZeroOrTwoRelations(a1, a2, b1,
b2);
        b2.setA(a2);
        checkForZeroOrTwoRelations(a1, a2, b1,
b2);
        b2.setA(null);
        checkForZeroOrTwoRelations(a1, a2, b1,
b2);
    }

    private static void
checkForZeroOrTwoRelations(A a1, A a2, B b1, B b2)
{
        int i = 0;
        if(a1.getB() != null) i++;
        if(a2.getB() != null) i++;
        if(b1.getA() != null) i++;
        if(b2.getA() != null) i++;
        if(i != 0 && i != 2) {
            throw new IllegalStateException();
        }
    }
}   

Categories : Java

Related to : How to keep a 1 to 1 relation consistent
Oracle - Difference betwen Consistent Gets in Autotrace and Consistent Read in SQL TRACE
The difference is that the tkprof output shows your single SELECT statement, but the Statistics section of autotrace output summarizes 2 statements, your SELECT plus 1 recursive calls, as indicated by the first line of that section. A few examples for recursive calls are: triggers (INSERT, UPDATE, DELETE), dynamic sampling (in case of missing table statistics) or internal statements querying data

Categories : Oracle
Cypher - Return nodes with their relation with a count contraint on the relation
Untested, but you should be able to unwind your rels collection and pull out the start and end nodes. Something like: MATCH (doc:document)-[contain]-(sentence)-[with]-(ng1:word)-[rel:relations]-(ng2:word) WITH doc, count(rel) as nbRels, collect(rel) as rels WHERE nbRels > 10 WITH doc, UNWIND(rels) as rel, RETURN doc, STARTNODE(rel), ENDNODE(rel) But you might just want to collect something d

Categories : Neo4j
SQL Server view to provide a consistent table structure using JSON
While not getting into the debate about whether this should be done in the database or not, it seemed like an interesting puzzle, and more and more people seem to be wanting at least simple JSON translation at the DB level. So, there are a couple of ideas. The first idea is to have SQL Server do most of the work for us by turning the row into XML via the FOR XML clause. A simple, attribute-based

Categories : C#
Insert / Update Records in MS SQL 2008 based on a second table, then keep dates consistent
I have two suggestions. First, I think in your "copying records" select, your last selected column should be B.DATA rather than A.DATA. Second, you name your temp table as #TEMP_TABLE. Using the hash prefix makes this an actual temp table, which will be created in tempdb and will pass out of scope when the session ends and won't be visible to other sessions. With those two tweaks, your code appe

Categories : Sql Server
Add new object to ActiveRecord::Relation
Convert @venues to array and add to array. @venues.build will return you a new object. but it will not automatically add to the collection. @venues = @venues.limit(30) if @venues.all.count < 30 #this converts @venues to array foursquare_client = foursquare_client() fq_venues = foursquare_client.explore_venues(ll: params[:ll], radius: 1000, limit: 30 - @venues.count(:all), section: 'foo

Categories : Ruby On Rails
Recently Add
Redirecting the output directory of 'mvn package' or 'mvn compile' command
No suitable constructor found for ProductoExtranjero
java Composite design pattern(Directory &File)
Java JTree's ui refresh after removing node from parent
First REST Spring application
How to cancel Indexing of a Solr document using Update Request Processor
PowerMock - Mock a Singleton with a Private Constructor
Calling a Postgres stored function SQL error
Where to store Morphlines Java custom command class?
Generic repository using map
How can I scroll a ScrolledComposited in Eclipse SWT Design view?
2 Frames/layout in 1 Activity
Writing a switch differently
Next button opens another activity when its reaches the array limit
Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?
Why my jdk can't work,and before the java_home, there is a space that is not from me
How to add List of objects in a Map
How to make notepad++ function like regular notepad in cmd?
Cell renderer and the lost focus
how can I implement iterable for LinkedList>
Disable Androids image-crunch in eclipse (run as) builds
java 8 lambda myMap.stream().count() != myMap.size() after merging myMap
Issue with Calendar calculation that spans 2 calendar years
JSF 2.0 Spring bean injection
Java Regex ReplaceAll with grouping
Getting any word and last word using sed
Clicking on link on JEditorPane throws IOException
printing out difference of two arrays
Spring Bean Alias in JavaConfig
Using Factory Method to Create Generics
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.