How to write two OneToMany relationships to one table in ActiveObjects

How do I write two OneToMany relationships to one table in ActiveObjects? Why is this test case failing?

simple message object.

import net.java.ao.Entity;

public interface Message extends Entity{
    public void setSender(Communicator sender);
    public void setAcceptor(Communicator acceptor);
}

      

simple communicator (human or server).

import net.java.ao.Entity;
import net.java.ao.OneToMany;

public interface Communicator extends Entity {
    @OneToMany
    public Message[] getSendMessages();

    @OneToMany
    public Message[] getAcceptMessages();

}

      

...

1.connect to mysql localhost.

2. Create table schemas.

3. Assemble the two sender and acceptor communicators.

4.Collect 10 messages as setSender (sender) and setAcceptor (acceptor).

5. Check the number of messages that the sender has received. it should be zero.

6.but junit says it is 10 but not zero.

import java.sql.SQLException;

import junit.framework.TestCase;
import net.java.ao.EntityManager;

public class AOTest2 extends TestCase{
    public void test() {
        String db_host = "localhost";
        String db_database = "test";
        String db_login = "root";
        String db_password = "";

        EntityManager m = new EntityManager("jdbc:mysql://" + db_host + "/" + db_database, db_login, db_password);

        try {
            m.migrate(Communicator.class, Message.class);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }

        try {
            Communicator sender = m.create(Communicator.class);
            Communicator acceptor = m.create(Communicator.class);
            sender.save();
            acceptor.save();
            for (int i = 0; i < 10; i++) {
                Message mes = m.create(Message.class);
                mes.setAcceptor(acceptor);
                mes.setSender(sender);
                mes.save();
            }

            assertEquals(true, sender.getAcceptMessages().length == 0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

      

Thank.

+3


source to share


1 answer


Maybe a little late, but in order to distinguish the two relationships, you need to specify the "reverse" attribute for OneToMany

@OneToMany(reverse = "methodDefiningTheRelationAtOtherClass")

      

Note: only supported since 0.22.1



see also: https://developer.atlassian.com/display/DOCS/OneToMany+Relationship

... "Set this recipient name to this on the remote interface. If you don't set these attributes, Active Objects will fallback to method output by type."

This way, if AO infers by type, it will accept the first relationship it can resolve - and that will always be the same for all of your OneToMany annotations to the same table.

+1


source







All Articles