您好,我在映射实体时遇到问题。我正在使用 JPA2 和 Hibernate 实现。我得到带有 @ManyToMany 注释的表
\n\nhttp://img204.imageshack.us/img204/7558/przykladd.png
\n\n我将其映射为:
\n\n@Entity\n@Table("employee")\nclass Employee {\n @Id\n \xc2\xa0\xc2\xa0@GeneratedValue(strategy = GenerationType.IDENTITY)\n \xc2\xa0\xc2\xa0private Integer id;\n\n\xc2\xa0\xc2\xa0@Column\n\xc2\xa0\xc2\xa0private String name;\xc2\xa0\n\n \xc2\xa0@ManyToMany\n\xc2\xa0\xc2\xa0@JoinTable(name = "proj_emp",\n\xc2\xa0 \xc2\xa0joinColumns = @JoinColumn(name = "employee_id", referencedColumnName = "id"),\n\xc2\xa0 inverseJoinColumns = @JoinColumn(name = "project_id", referencedColumnName = "id"),\xc2\xa0\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0uniqueConstraints = @UniqueConstraint(columnNames = {"employee_id", "project_id"}))\xc2\xa0\n\xc2\xa0\xc2\xa0private List<Project> projects;\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0...}\n\n\n@Entity\n@Table("project")\nclass Project {\n\xc2\xa0\xc2\xa0\xc2\xa0@Id\n\xc2\xa0\xc2\xa0\xc2\xa0@GeneratedValue(strategy = GenerationType.IDENTITY)\n\xc2\xa0\xc2\xa0\xc2\xa0private Integer id;\xc2\xa0\n\n\xc2\xa0\xc2\xa0\xc2\xa0@Column\xc2\xa0\xc2\xa0\n \xc2\xa0\xc2\xa0private String name;\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\n\xc2\xa0\xc2\xa0\xc2\xa0@Column\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\n\xc2\xa0\xc2\xa0\xc2\xa0private Integer budget;\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\n\n\xc2\xa0\xc2\xa0\xc2\xa0@ManyToMany(mappedBy = "projects")\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\n\xc2\xa0\xc2\xa0\xc2\xa0private List<Employee> employees;\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0...}\nRun Code Online (Sandbox Code Playgroud)\n\n现在,当我从 Employee 中删除记录时,我希望从表 proj_emp 中进行级联删除,但表 Project 中的任何内容都无法删除。
\n\n获得它的最佳方式是什么?
\n\n谢谢\n大卫
\n我正在使用JPA2和Hibernate实现.
我有这样的简单映射:
@Entity
class Topic {
@Id
@GeneratedValue(strategy = IDENTITY)
int id;
@OneToOne(cascade = ALL)
@JoinColumn(name = "id_poll")
private Poll poll;
}
@Entity
class Poll {
@Id
@GeneratedValue(strategy = IDENTITY)
int id;
}
Run Code Online (Sandbox Code Playgroud)
现在,当我删除一个也在Topic中的Poll对象时,我收到一个错误.
java.sql.SQLException:完整性约束违规FKCC42D924982D3F4B表:语句中的TOPICS [从民意调查中删除id =?]
我理解这是因为如果Poll记录在另一个表中有引用,我就无法删除它.我怎么解决这个问题?我是否必须在主题表中手动设置poll = null或者是否有更好的解决方案?
我想知道是否有可能向JQuery新选项添加其他数据.我有一些选择对象,我追加动态内容:
select.append(new Option(item.name, item.id))
Run Code Online (Sandbox Code Playgroud)
它产生一个HTML,例如:
<option value="22">Uniqa OC Zawodu</option>
Run Code Online (Sandbox Code Playgroud)
我想在选项中添加其他数据,因此我的HTML看起来像:
<option value="22" data-url="some_data">Uniqa OC Zawodu</option>
Run Code Online (Sandbox Code Playgroud) 我想使用jQuery交换两个链接中的类.我有一个HTML代码,如:
<div class="showHide1">
<a id="aaa" class="show">AAA</a>
<a id="bbb" class="hide">BBB</a>
</div>
<div class="showHide2">
<a id="aaa" class="show">AAA</a>
<a id="bbb" class="hide">BBB</a>
</div>
Run Code Online (Sandbox Code Playgroud)
和一个简单的.css文件:
.show {
display: block;
}
.hide {
display: none;
}
Run Code Online (Sandbox Code Playgroud)
现在我想在点击"showHide"div上的某个地方后交换两个链接中的类.
$('#showHide1').click(function() {
if ($("#aaa").hasClass('show')) {
$("#aaa").attr("class","hide");
}
else {
$("#aaa").attr("class","show");
}
if ($("#bbb").hasClass('hide')) {
$("#bbb").attr("class","show");
}
else {
$("#bbb").attr("class","hide");
} `
Run Code Online (Sandbox Code Playgroud)
我确信有更好的方法来解决这个问题,因为这只适用于"showHide1"div,我必须复制几乎相同的代码才能使它适用于"showHide2"div.有谁能告诉我一个更好的解决方案?
谢谢
的Dawid
cascade ×2
hibernate ×2
jpa-2.0 ×2
jquery ×2
annotations ×1
class ×1
html ×1
javascript ×1
one-to-one ×1