当我尝试使用createQuery
从数据库中读取时,我收到以下错误,但如果我在createNativeQuery
上使用entityManager
,我可以从数据库中读取。所以我猜我的entityManager
有效但我的Hibernate配置有问题吗?
application.properties:
security.user.password=<password>
logging.level.org.springframework.security=DEBUG
spring.datasource.url=jdbc:jtds:sqlserver://localhost:60830;databaseName=<db>
spring.datasource.username=<user>
spring.datasource.password=<password>
spring.datasource.driver-class-name=net.sourceforge.jtds.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServerDialect
spring.jpa.database-platform=org.hibernate.dialect.SQLServerDialect
DAO:
package demo.dao;
import demo.entities.StampCard;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Transactional
@Repository
public class StampCardDAO implements IStampCardDAO {
@PersistenceContext
private EntityManager entityManager;
@Override
public StampCard getStampCard(int id) {
//These rows works fine
//Query query = entityManager.createNativeQuery("select userId from StampCard where StampCardID = '1'");
//String userId = (String) query.getSingleResult();
Query query = entityManager.createQuery("SELECT s FROM StampCard s WHERE s.stampCardID = '1'");
StampCard stampCard = (StampCard) query.getSingleResult();
return stampCard;
}
}
DAO接口:
package demo.dao;
import demo.entities.StampCard;
public interface IStampCardDAO {
StampCard getStampCard(int id);
}
实体:
package demo.entities;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
@Entity
public class StampCard implements Serializable {
private static final long serialVersionUID = 6602888822739626415L;
private int stampCardID;
@Getter @Setter private String createdDate;
@Getter @Setter private String userID;
@Getter @Setter private int numberOfStamps;
@Id
public int getStampCardID() {
return stampCardID;
}
public void setStampCardID(int stampCardID) {
this.stampCardID = stampCardID;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (obj.getClass() != getClass()) {
return false;
}
StampCard rhs = (StampCard) obj;
return new EqualsBuilder().append(this.stampCardID, rhs.stampCardID).append(this.createdDate, rhs.createdDate).append(this.userID, rhs.userID)
.append(this.numberOfStamps, rhs.numberOfStamps).isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(stampCardID).append(createdDate).append(userID).append(numberOfStamps).toHashCode();
}
}
数据库表:
CREATE TABLE [dbo].[StampCard](
[StampCardID] [int] NOT NULL,
[CreatedDate] [varchar](8) NOT NULL,
[UserID] [varchar](255) NOT NULL,
[NumberOfStamps] [int] NOT NULL,
CONSTRAINT [PK_CardID] PRIMARY KEY CLUSTERED
(
[StampCardID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
数据库表内容:
StampCardID CreatedDate UserID NumberOfStamps
1 20171208 USer 1
错误讯息:
2017-12-10 18:04:09.837 WARN 37080 --- [restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:208,SQLState: S0002 2017-12-10 18:04:09.840 ERROR 37080 --- [restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper:无效的对象名称 'stamp_card'。
引起:java.sql.SQLException:无效的对象名称'stamp_card'。
我感谢能得到的所有帮助。
答案 0 :(得分:3)
@Entity
,hibernate搜索名为Stamp_Card
的表
因此,如果您的表名只是StampCard
,请使用如下所示的表格注释来指向您的表名。
@Entity
@Table(name = "STAMPCARD")
答案 1 :(得分:3)
接受的答案有效。另一种解决方案是改变命名策略。 Springs默认命名策略org.springframework.boot.orm.jpa.SpringNamingStrategy
,使用下划线拆分驼峰案例名称。通过在application.properties中添加spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
来更改命名策略。然后不需要@Table
注释,因为数据库表和实体类具有相同的名称。
答案 2 :(得分:0)
我有一个类似的问题,但就我而言,根本原因是我连接到的数据库中的现有架构。
在配置(application.properties文件)中添加以下行可以完全删除现有模式并创建通过我的应用程序定义的模式
spring.jpa.hibernate.ddl-auto =创建-放置
以下是Naros的回答,它帮助我更好地理解了“ create-drop ”属性: https://stackoverflow.com/a/42147995/6110987
希望有帮助!
答案 3 :(得分:0)
对于您看到该列不存在的情况。可能是在数据库中您的列名类似于JiraNumber的原因,因此在@Table注释以及@Column注释中都在大写中写出了参数名的字符串值。