2012-10-27 1 views
0

저는 Play to new입니다. Canteen과 OpeningTimes간에 OneToOne 관계를 성취하려고합니다. 1 : 1이 내가 원하는 것 같습니다. 나는 NullPointerException이 무엇입니까하지만 ... 여기Play! 2.0 - OneToOne 관계 - NullPointerException

@Entity 
public class OpeningTimes extends Model { 

    @Id 
    public Long id; 

    @Required 
    public String mondayHours; 

    @Required 
    public String tuesdayHours; 

     ... 

    @OneToOne 
    public Canteen canteen; 

}

@Entity 
public class Canteen extends Model { 

    @Id 
    public Long id; 

    ... 

    @OneToOne 
    public OpeningTimes openingTimes; 

} 

나는 데이터베이스를

public class Global extends GlobalSettings { 

@Override 
public void onStart(Application arg0) { 
    if(Canteen.finder.findRowCount() == 0) { 

     Canteen canteen = new Canteen(); 
     ... 
     canteen.save(); 


     Canteen canteen1 = new Canteen(); 
     ... 
     canteen1.save(); 


     OpeningTimes times = new OpeningTimes(); 
     times.mondayHours = "7:00 - 15:00"; 
     times.tuesdayHours = "7:00 - 15:00"; 
     ... 
     times.canteen = canteen; 
     times.save(); 


     OpeningTimes times1 = new OpeningTimes(); 
     times1.mondayHours = "7:00 - 15:00"; 
     times1.tuesdayHours = "7:00 - 15:00"; 
     ... 
     times1.canteen = canteen1; 
     times1.save(); 

} 

show.scala.html

@(canteen: Canteen) 

@main("MamHlad | " + canteen.nameShort) { 

    <h1>@canteen.nameFull</h1> 

    <h2>Opening times</h2> 
    ** <p>@canteen.openingTimes.mondayHours</p> ** 
    <p>@canteen.openingTimes.tuesdayHours</p> 
} 

Asterixes 마크를 채울 그것이 던지는 줄 그는 NullPointerException, 여기에 스택 추적이 있습니다

! @6c6cgmg4b - Internal server error, for request [GET /canteens/2] -> 

play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[NullPointerException: null]] 
     at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:134) [play_2.9.1.jar:2.0.4] 
     at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:115) [play_2.9.1.jar:2.0.4] 
     at akka.actor.Actor$class.apply(Actor.scala:318) [akka-actor.jar:2.0.2] 
     at play.core.ActionInvoker.apply(Invoker.scala:113) [play_2.9.1.jar:2.0.4] 
     at akka.actor.ActorCell.invoke(ActorCell.scala:626) [akka-actor.jar:2.0.2] 
     at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197) [akka-actor.jar:2.0.2] 
Caused by: java.lang.NullPointerException: null 
     at views.html.show$.apply(show.template.scala:37) ~[classes/:na] 
     at views.html.show$.render(show.template.scala:105) ~[classes/:na] 
     at views.html.show.render(show.template.scala) ~[classes/:na] 
     at controllers.Application.show(Application.java:23) ~[classes/:na] 
     at Routes$$anonfun$routes$1$$anonfun$apply$5$$anonfun$apply$6.apply(routes_routing.scala:76) ~[classes/:na] 
     at Routes$$anonfun$routes$1$$anonfun$apply$5$$anonfun$apply$6.apply(routes_routing.scala:76) ~[classes/:na] 

감사합니다.

+0

단계 :이처럼, 여기

canteen.setOpeningTimes(openingTime1); canteen.save(); 

은 예입니다. 'canteen' 또는'canteen.openingTimes'는'null'입니다. –

답변

2

OneToOne 관계의 소유면을 지정하지 않았 음을 알았습니다. 필수 항목입니다.

@Entity 
public class OpeningTimes extends Model { 

    @Id 
    public Long id; 
     ... 

    @OneToOne(mappedBy="openingTimes", cascade=CascadeType.ALL) 
    public Canteen canteen; 
} 

또한 속성을 직접 액세스하는 대신 setOpeningTimes 메서드를 사용해야한다고 생각합니다. 디버거를 사용하여 코드를 통해 https://ebeanorm.svn.sourceforge.net/svnroot/ebeanorm/ebean/trunk/src/test/java/com/avaje/tests/basic/TestMultipleOneToOneIUD.java

+0

nullpointer bude 그냥 아무 것도 렌더링하지 않는다 ... wtf – urSus