1

我有一个非常简单的模型,名为“Scholar”,带有相应的 getter/setter 方法。我还有一种方法 findByFullName(String name, String surname) 可以根据输入的姓名或姓氏检索列表。
@Entity
@Table(name="scholar")
public class Scholar extends Model {
@Id
@Column(name = "student_number")
private long studentNumber;
@Column(name = "surname")
private String surname;
@Column(name = "name")
private String name;

我的 findByFullName 方法如下:
public static List<Scholar> findByFullName(String name, String surname) {
List<Scholar> returned = JPA.em().createQuery("from Scholar s where s.surname=?") .setParameter(1, surname).getResultList();

生成的 Scholar List 包含只有 studentNumber 的 Scholar 对象。即使学者表具有两列的数据,姓名和姓氏也会返回 null。IE

returned.get(0).getStudentNumber returns a value
returned.get(0).getName() returns null
returned.get(0).getSurname() returns null

异常跟踪:
内部服务器错误,请求 [POST /search] ->

play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[NullPo
interException: null]]
at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:134) [
play_2.9.1.jar:2.0.3]
at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:115) [
play_2.9.1.jar:2.0.3]
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.3]
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 models.Scholar._ebean_get_surname(Scholar.java:4) ~[classes/:na]
    at models.Scholar.getSurname(Scholar.java:89) ~[classes/:na]
    at models.Scholar.findbyIdOrNames(Scholar.java:55) ~[classes/:na]
    at controllers.Search.findScholar(Search.java:30) ~[classes/:2.0.3]
    at Routes$$anonfun$routes$1$$anonfun$apply$11$$anonfun$apply$12.apply(ro
     utes_routing.scala:92) ~[classes/:na]
    at Routes$$anonfun$routes$1$$anonfun$apply$11$$anonfun$apply$12.apply(ro
    utes_routing.scala:92) ~[classes/:na]

我的 persistence.xml 看起来像:

    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence        http://java.sun.com    /xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>

</persistence>

我的 Application.conf 文件如下所示:

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/DBName" 
db.default.user=user
db.default.password=password
jpa.default=defaultPersistenceUnit

db.default.jndiName=DefaultD`

我想检索所有 Scholar 属性;studentNumber, name and surname 请协助,字面上卡住没有前进的路

4

0 回答 0