2 回答

TA貢獻2011條經(jīng)驗 獲得超2個贊
@Entity
@Table(name="Person") 公共類人 {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="person_id")
private long personId;
@Column(name="name")
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
// getters and setters
}
您需要向該實體添加更多注釋,如上述代碼中所述。1)@Column 以便hibernate了解哪個列映射到實體中的哪個屬性(如果列名和屬性名相同,則不需要這樣做)。表列名如上所述需要提及。

TA貢獻1840條經(jīng)驗 獲得超5個贊
我在這里要做的就是在啟動和停止服務器之間保留數(shù)據(jù),并能夠通過 SQL 數(shù)據(jù)庫訪問數(shù)據(jù)。我通過指定您希望 Hibernate 使用的數(shù)據(jù)庫,在沒有使用@Transactional
或Session
接口的情況下解決了這個問題。在這里找到- 感謝@Master Slave。
腳步:
啟動我的 SQL 服務器并創(chuàng)建數(shù)據(jù)庫
添加一個
application.properties
文件來配置 Spring 以使用該數(shù)據(jù)庫第一次運行應用程序時,設置
spring.jpa.hibernate.ddl-auto =
為create
. 下一次,將其設置為update
. 這將在會話中保留該數(shù)據(jù)。
application.properties: -僅在第一次運行時使用:
spring.datasource.url=jdbc:mysql://localhost:8889/my_db
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
之后bootRun,my_db將填充數(shù)據(jù)。停止您的 Spring 服務器并重新啟動它,但這次spring.jpa.hibernate.ddl-auto=update在您的application.properties.
希望這可以幫助遇到類似問題的其他人。
添加回答
舉報