-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
NOT optional on other side
- Loading branch information
Showing
9 changed files
with
220 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.avaje.tests.model.onetoone; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.OneToOne; | ||
import javax.persistence.Table; | ||
|
||
import com.avaje.tests.model.BaseModel; | ||
|
||
@Entity | ||
@Table(name="oto_account") | ||
public class Account extends BaseModel { | ||
|
||
public static final Finder<Long,Account> find = new Finder<Long,Account>(Long.class, Account.class); | ||
|
||
String name; | ||
|
||
@OneToOne(mappedBy = "account",optional = true) | ||
User user; | ||
|
||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public User getUser() { | ||
return user; | ||
} | ||
|
||
public void setUser(User user) { | ||
this.user = user; | ||
} | ||
|
||
} |
117 changes: 117 additions & 0 deletions
117
src/test/java/com/avaje/tests/model/onetoone/TestOneToOneOptionalRelationship.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.avaje.tests.model.onetoone; | ||
|
||
import java.util.List; | ||
|
||
import org.avaje.ebeantest.LoggedSqlCollector; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import com.avaje.ebean.BaseTestCase; | ||
import com.avaje.ebean.config.GlobalProperties; | ||
|
||
public class TestOneToOneOptionalRelationship extends BaseTestCase { | ||
|
||
@Test | ||
public void test() { | ||
|
||
GlobalProperties.put("ebean.search.packages", "com.avaje.tests.model.onetoone"); | ||
|
||
Account account = new Account(); | ||
account.setName("AC234"); | ||
account.save(); | ||
|
||
LoggedSqlCollector.start(); | ||
|
||
Account fetchedAccount = Account.find.byId(account.getId()); | ||
Assert.assertNotNull(fetchedAccount); | ||
|
||
List<String> loggedSql = LoggedSqlCollector.stop(); | ||
Assert.assertEquals(1, loggedSql.size()); | ||
|
||
// select t0.id c0, t0.name c1, t0.version c2, t0.when_created c3, t0.when_updated c4, t1.id c5 | ||
// from oto_account t0 | ||
// join oto_user t1 on t1.account_id = t0.id | ||
// where t0.id = ? | ||
|
||
String sql = loggedSql.get(0); | ||
Assert.assertTrue(sql.contains("select t0.id c0, t0.name c1")); | ||
Assert.assertTrue(sql.contains(" from oto_account t0 left outer join oto_user t1 on t1.account_id = t0.id where t0.id = ?")); | ||
} | ||
|
||
|
||
@Test | ||
public void testWithUser() { | ||
|
||
GlobalProperties.put("ebean.search.packages", "com.avaje.tests.model.onetoone"); | ||
|
||
Account account = new Account(); | ||
account.setName("AC678"); | ||
account.save(); | ||
|
||
User user = new User(); | ||
user.setName("Geoff"); | ||
user.setAccount(account); | ||
user.save(); | ||
|
||
LoggedSqlCollector.start(); | ||
|
||
Account fetchedAccount = Account.find.byId(account.getId()); | ||
Assert.assertNotNull(fetchedAccount); | ||
|
||
Assert.assertNotNull(fetchedAccount.getUser()); | ||
Assert.assertEquals(user.getId(), fetchedAccount.getUser().getId()); | ||
Assert.assertEquals(user.getName(), fetchedAccount.getUser().getName()); | ||
|
||
List<String> loggedSql = LoggedSqlCollector.stop(); | ||
Assert.assertEquals(2, loggedSql.size()); | ||
|
||
// select t0.id c0, t0.name c1, t0.version c2, t0.when_created c3, t0.when_updated c4, t1.id c5 | ||
// from oto_account t0 | ||
// join oto_user t1 on t1.account_id = t0.id | ||
// where t0.id = ? | ||
|
||
String sql = loggedSql.get(0); | ||
Assert.assertTrue(sql.contains("select t0.id c0, t0.name c1")); | ||
Assert.assertTrue(sql.contains(" from oto_account t0 left outer join oto_user t1 on t1.account_id = t0.id where t0.id = ?")); | ||
|
||
String lazyLoadSql = loggedSql.get(1); | ||
Assert.assertTrue(lazyLoadSql.contains("select t0.id c0, t0.name c1, t0.version c2, t0.when_created c3, t0.when_updated c4, t0.account_id c5 from oto_user t0 where t0.id = ?")); | ||
} | ||
|
||
|
||
@Test | ||
public void testWithUserFetch() { | ||
|
||
GlobalProperties.put("ebean.search.packages", "com.avaje.tests.model.onetoone"); | ||
|
||
Account account = new Account(); | ||
account.setName("AC786"); | ||
account.save(); | ||
|
||
User user = new User(); | ||
user.setName("Jane"); | ||
user.setAccount(account); | ||
user.save(); | ||
|
||
LoggedSqlCollector.start(); | ||
|
||
Account fetchedAccount = Account.find.fetch("user").setId(account.getId()).findUnique(); | ||
Assert.assertNotNull(fetchedAccount); | ||
|
||
Assert.assertNotNull(fetchedAccount.getUser()); | ||
Assert.assertEquals(user.getId(), fetchedAccount.getUser().getId()); | ||
Assert.assertEquals(user.getName(), fetchedAccount.getUser().getName()); | ||
|
||
List<String> loggedSql = LoggedSqlCollector.stop(); | ||
Assert.assertEquals(1, loggedSql.size()); | ||
|
||
// select t0.id c0, t0.name c1, t0.version c2, t0.when_created c3, t0.when_updated c4, t1.id c5 | ||
// from oto_account t0 | ||
// join oto_user t1 on t1.account_id = t0.id | ||
// where t0.id = ? | ||
|
||
String sql = loggedSql.get(0); | ||
Assert.assertTrue(sql.contains("select t0.id c0, t0.name c1")); | ||
Assert.assertTrue(sql.contains(" from oto_account t0 left outer join oto_user t1 on t1.account_id = t0.id where t0.id = ?")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.avaje.tests.model.onetoone; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.OneToOne; | ||
import javax.persistence.Table; | ||
|
||
import com.avaje.tests.model.BaseModel; | ||
|
||
@Entity | ||
@Table(name="oto_user") | ||
public class User extends BaseModel { | ||
|
||
public static final Finder<Long,User> find = new Finder<Long,User>(Long.class, User.class); | ||
|
||
String name; | ||
|
||
@OneToOne(optional = false) | ||
Account account; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Account getAccount() { | ||
return account; | ||
} | ||
|
||
public void setAccount(Account account) { | ||
this.account = account; | ||
} | ||
|
||
} |