我正在转换jpa实体以使用lombok。结果代码如下:
@Entity
@Table(name = "TEST")
@Data
@NoArgsConstructor
@AllArgsConstructor
class Test {
...
@Column(name = "FORMATTING")
@Enumerated(EnumType.ORDINAL)
private FormatType formatType;
...
}
Run Code Online (Sandbox Code Playgroud)
结果错误消息包含以下内容
Caused by: org.hibernate.HibernateException: Missing column: formatType in TEST
Run Code Online (Sandbox Code Playgroud)
我真的不确定在这里用谷歌搜索。(我尝试过将所有内容粘贴formatType到Google中-没看到任何内容)
为了简洁和隐私起见,已将字段重新命名,并省略了看起来不相关的方面。如果看起来像错字,那可能就是错字。请让我知道,如果您注意到什么,以便我可以解决。
与我正在使用的代码相比,描述该字段的3行没有变化
我刚刚在错误消息之前注意到了这一点
13:22:19,967 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 57) HHH000261: Table found: TABLE
13:22:19,967 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 57) HHH000037: Columns: [..., formatType, ...]
13:22:19,968 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 57) MSC000001: Failed to start service jboss.persistenceunit."...": org.jboss.msc.service.StartException in service jboss.persistenceunit."...": javax.persistence.PersistenceException: [PersistenceUnit: ...] Unable to build EntityManagerFactory
Run Code Online (Sandbox Code Playgroud)
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "PARENT")
public abstract class Parent implements Serializable {
private static final long serialVersionUID = 1;
@Id
@Column(name = "ID")
@GeneratedValue
private long id;
@Column(name = "ENABLED")
private boolean enabled;
}
@Entity
@Table(name = "CHILD")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Child extends Parent {
/** XXX: HERE BE DRAGONS */
@Column(name = "ENUM_1")
@Enumerated(EnumType.STRING)
private Enum1 enum1;
@Column(name = "ENUM_2")
@Enumerated(EnumType.ORDINAL)
private Enum2 enum2;
/** XXX: NO MORE DRAGONS */
@Column(name = "FREQUENCY")
private String frequency;
@Column(name = "EMPTY")
private boolean empty;
@Column(name = "MAX_SIZE")
private int maxSize;
}
public enum Enum1 {
A,
B,
C
}
public enum Enum2 {
X,
Y,
Z
}
Run Code Online (Sandbox Code Playgroud)
我已经回退了龙目岛的更改,我仍然想知道问题出在哪里,但不要着急。另外,由于这个可爱的小虫子,我比我晚了大约4个小时,所以我的回复可能会有点慢。
子表的pk是父表的fk,并且没有lombok的所有东西似乎都可以工作,尽管Child该类没有id。
解:
我完全忘了问这个。不久前,我重新讨论了这个问题。为了解释该解决方案,让我们看一下我包括的第一个示例的稍微简化的版本。
@Entity
@Table(name = "TEST")
@Setter
@Getter
class Test {
...
@Column(name = "FORMATTING")
@Enumerated(EnumType.ORDINAL)
private FormatType formatType;
...
}
Run Code Online (Sandbox Code Playgroud)
看来龙目岛会给你这个:
@Entity
@Table(name = "TEST")
class Test {
...
@Column(name = "FORMATTING")
@Enumerated(EnumType.ORDINAL)
private FormatType formatType;
public FormatType getFormatType() {
return formatType;
}
public void setFormatType(FormatType formatType) {
this.formatType = formatType;
}
...
}
Run Code Online (Sandbox Code Playgroud)
请注意,注释仍附加在字段上。现在,我不确定是我们使用的只是JPA的版本还是实现,但是我收集到,如果定义了访问器,jpa只会忽略除@Column(以及为该参数指定的任何参数之外)的任何注释,@Column这就是jpa正在查找的原因错误的列名)。因此,我们实际上需要:
@Entity
@Table(name = "TEST")
class Test {
...
private FormatType formatType;
@Column(name = "FORMATTING")
@Enumerated(EnumType.ORDINAL)
public FormatType getFormatType() {
return formatType;
}
public void setFormatType(FormatType formatType) {
this.formatType = formatType;
}
...
}
Run Code Online (Sandbox Code Playgroud)
经过大量的困惑,试图找到示例并填写有关龙目岛如何做事情的一些细节(公平地说,我很容易混淆),我发现了这个小宝石:onMethod=@__({@AnnotationsHere})。利用此功能,我想到了以下内容:
@Entity
@Table(name = "TEST")
@Setter
class Test {
...
@Getter(onMethod=@__({
@Column(name = "FORMATTING"),
@Enumerated(EnumType.ORDINAL)
}))
private FormatType formatType;
...
}
Run Code Online (Sandbox Code Playgroud)
并能正常工作。现在,我们有了显然是我想解决的唯一唯一可用的解决方案,我们现在正在思考的问题:除了手动编写方法并在其中附加注释之外,真的还有其他更清洁的方法吗?答:...我不知道。我很高兴找到解决方案。
小智 3
真奇怪。你能展示更多代码吗?我正在尝试编写一个简单的项目,其中包含您问题中的部分代码,并且它有效。我使用了 Spring Boot 和 MySQL。尝试检查您的配置。有我的代码:
枚举:
public enum FormatType {
FIRST_TYPE, SECOND_TYPE
}
Run Code Online (Sandbox Code Playgroud)
MySQL 中的表:
create table TEST
(
ID int auto_increment primary key,
FORMATTING int not null
);
Run Code Online (Sandbox Code Playgroud)
实体:
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Entity
@Table(name = "TEST")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Test {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "FORMATTING")
@Enumerated(EnumType.ORDINAL)
private FormatType formatType;
}
Run Code Online (Sandbox Code Playgroud)
存储库:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TestRepository extends JpaRepository<Test, Integer> {
}
Run Code Online (Sandbox Code Playgroud)
服务:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TestService {
private TestRepository repository;
@Autowired
public TestService(TestRepository repository) {
this.repository = repository;
}
public List<Test> getAllTestEntities() {
return repository.findAll();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9014 次 |
| 最近记录: |