QuerySyntaxException:新闻通讯未映射

时间:2019-03-06 15:24:27

标签: java hibernate jpa

在部署应用程序时出现错误。 我收到此错误: 由以下原因引起:org.hibernate.hql.internal.ast.QuerySyntaxException:新闻通讯未映射[SELECT n FROM Newsletter n where n.user.username =:username]

这是 com.app.domain.Newsletter

@Entity(name="NEWSLETTER")
public class Newsletter {

  @Id
  @GeneratedValue(generator = "user_id_gen", strategy = GenerationType.SEQUENCE)
  @SequenceGenerator(name = "user_id_gen", sequenceName = "user_id_seq", allocationSize = 1, initialValue = 1)
  protected Long id;


  @OneToOne(mappedBy = "newsletter")
  @JoinColumn(name = "id_user", referencedColumnName = "id")
  private User user;

  @Column(name = "D_SUBSCRIBE_DATE")
  @Temporal(TemporalType.TIMESTAMP)
  private Date subscribeDate;

  @Column(name = "B_ISSUB")
  private Boolean isSub;

  /**
   *
   * @return
   */
  public User getUser() {
    return user;
  }

  /**
   *
   * @param user
   */
  public void setUser(User user) {
    this.user = user;
  }

  /**
   *
   * @return
   */
  public Date getSubscribeDate() {
    return subscribeDate;
  }

  /**
   *
   * @param subscribeDate
   */
  public void setSubscribeDate(Date subscribeDate) {
    this.subscribeDate = subscribeDate;
  }

  /**
   *
   * @return
   */
  public Boolean getSub() {
    return isSub;
  }

  /**
   *
   * @param sub
   */
  public void setSub(Boolean sub) {
    isSub = sub;
  }

}

这是 com.app.domain.User

    @Entity(name="USER")
public class User {

    @Id
    @GeneratedValue(generator = "user_id_gen", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "user_id_gen", sequenceName = "user_id_seq", allocationSize = 1, initialValue = 1)
    private Long id;
    @Column(name="USERNAME", nullable=false, length=20)
    private String username;
    @Column(name="PASSWORD_HASH", nullable=false, length=128)
    private String passWordhash;
    @Column(name="FIRST_NAME", nullable=false, length=50)
    private String firstName;
    @Column(name="LAST_NAME", nullable=false, length=50)
    private String lastName;
    private LocalDate birthDay;
    private String address;
    private String city;
    private Integer postCode;
    private Integer rn;
    @Column(nullable=false, length=100)
    private String email;
    private String phoneNumber;

  @OneToOne
  private Newsletter newsletter;

    public Long getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public LocalDate getBirthDay() {
        return birthDay;
    }

    public String getAddress() {
        return address;
    }

    public String getCity() {
        return city;
    }

    public Integer getPostCode() {
        return postCode;
    }

    public Integer getRn() {
        return rn;
    }

    public String getEmail() {
        return email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setBirthDay(LocalDate birthDay) {
        this.birthDay = birthDay;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setPostCode(Integer postCode) {
        this.postCode = postCode;
    }

    public void setRn(Integer rn) {
        this.rn = rn;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getPassWordhash() {
        return passWordhash;
    }

    public User setPassWordhash(String passWordhash) {
        this.passWordhash = passWordhash;
        return this;
    }

    public String getUsername() {
        return username;
    }

    public User setUsername(String username) {
        this.username = username;
        return this;
    }

  public Newsletter getNewsletter() {
    return newsletter;
  }

  public void setNewsletter(Newsletter newsletter) {
    this.newsletter = newsletter;
  }
}

这是我的时事通讯:

    @Transactional
public interface NewsletterDao extends JpaRepository<Newsletter, Long> {

  @Query("SELECT n FROM Newsletter n where n.user.username = :username")
  Optional<Newsletter> findByUsername(@Param("username") String username);

}

这是我的persistence.xml文件配置。

    <?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
    version="2.2">

    <persistence-unit name="test">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <mapping-file>META-INF/orm.xml</mapping-file>
        <class>com.app.domain.User</class>
    <class>com.app.domain.Newsletter</class>
    </persistence-unit>

</persistence>

我收到此错误:

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Newsletter is not mapped [SELECT n FROM Newsletter n where n.user.username = :username]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:713)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:103)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350)
at com.sun.proxy.$Proxy45.createQuery(Unknown Source)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:87)
... 82 more

有人可以帮助我吗?我有点迷路了。

谢谢!

2 个答案:

答案 0 :(得分:0)

将时事通讯类添加到映射文件orm.xml

答案 1 :(得分:0)

我与OP合作。 从name中删除@Entity参数确实会给我们同样的错误。 这是我们的orm.xml文件:

      <?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd">

  <persistence-unit-metadata>
        <persistence-unit-defaults>
            <schema>public</schema>
        </persistence-unit-defaults>
    </persistence-unit-metadata>   

</entity-mappings>

我们做错了什么吗?谢谢!