JPA @ManyToOne保存问题

时间:2018-02-12 05:44:21

标签: mysql jpa spring-data-jpa mariadb

我有两个名为' Group'和'通用'。当我尝试使用Group的引用在mySql中保存通用数据时,它将新记录保存到组表,然后在Generic表中使用此记录。如何解决这个问题?

集团实体:

@Entity
@Table(name = "mdcn_group")
public class Group extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
protected Long id;

private String name;
private int status;
private String comments;

public Group() {
}

public Group(long id){
    this.id = id;
}

public Group(long id, String name) {
    this.id = id;
    this.name = name;
}
// ... getter and setter

通用实体:

@Entity
@Table(name = "generic")
public class Generic extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
protected Long id;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "group_id")
private Group group;
private String name;
private int status;
private String comments;

public Generic(){}

public Generic(long id){
    this.id = id;
}

public Generic(long id, String name){
    this.id = id;
    this.name = name;
}
// ... getter and setter

3 个答案:

答案 0 :(得分:2)

我认为您的方言配置与您的数据库不匹配。我有时会遇到这个问题。

转到您的 application.properties或配置文件 ,将休眠方言“ MySQL5Dialect”更改为“ MySQL5InnoDBDialect”。

#hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

答案 1 :(得分:0)

我没有看到您的群组实体中映射的通用实体。请参阅here

请在群组实体类中添加以下映射并尝试。

[ 61%] Linking CXX executable vsomeipd
../libvsomeip.so.2.0.1: error: undefined reference to 
'boost::filesystem::detail::dir_itr_close(void*&, void*&)'
../libvsomeip.so.2.0.1: error: undefined reference to 'boost::filesystem::detail::directory_iterator_construct(boost::filesystem::directory_iterator&, boost::filesystem::path const&, boost::system::error_code*)'
../libvsomeip.so.2.0.1: error: undefined reference to 'boost::filesystem::detail::directory_iterator_increment(boost::filesystem::directory_iterator&, boost::system::error_code*)'
../libvsomeip.so.2.0.1: error: undefined reference to ' 
'boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
clang50++: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: [daemon/vsomeipd] Error 1
make[1]: [daemon/CMakeFiles/vsomeipd.dir/all] Error 2
make[1]: Waiting for unfinished jobs....
[98%] Built target vsomeip-sd
make: [all] Error 2

答案 2 :(得分:0)

级联中的问题。请从您的通用对象中删除级联。

@ManyToOne
@JoinColumn(name = "group_id")
private Group group;