How can I write two OneToMany relation to same table in ActiveObjects? Why this test case fails?
simple message entity.
import net.java.ao.Entity;
public interface Message extends Entity{
public void setSender(Communicator sender);
public void setAcceptor(Communicator acceptor);
}
simple communicator(person 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();
}
test case.
1.connect to mysql of localhost.
2.create table schemas.
3.create two communicator of sender and acceptor.
4.create 10 messages as setSender(sender) and setAcceptor(acceptor).
5.check the number of the messages which the sender accepted. 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 you.
Might be a bit late, but in order to distinguish the two relations you need to specify the "reverse" attribute for OneToMany
Note: This is only supported since 0.22.1
see also: https://developer.atlassian.com/display/DOCS/OneToMany+Relationship
Thus, if AO inferrs by type, it will take the first relation it can resolve - and this will always be the same one for all your OneToMany annotations to the same table.