问题描述
public class距离{
私人课程courseA ,courseB;
private int minDistance;
双重成本;
私人长ID;
public Distance(){
super();
公共距离(课程courseA,课程courseB,int minDistance,双重费用){
super();
this.courseA = courseA;
this.courseB = courseB;
this.minDistance = minDistance;
this.cost =费用;
$ b @Override
public String toString(){
returnDistance [courseA =+ courseA +,courseB =+ courseB
+,MinDistance =+ minDistance +,Cost =+ cost +];
}
公开课程getCourseA(){
return courseA;
}
public void setCourseA(Course courseA){
this.courseA = courseA;
}
公开课程getCourseB(){
return courseB;
}
public void setCourseB(Course courseB){
this.courseB = courseB;
}
public int getMinDistance(){
return minDistance;
}
public void setMinDistance(int minDistance){
this.minDistance = minDistance;
}
public double getCost(){
return cost;
}
public void setCost(double cost){
this.cost = cost;
}
@Id
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
}
Where Course是我创建的另一个课程:
公开课程{b
$ b私人长ID;
私人字符串名称;
私人日历日期;
public Course(){
super();
}
public Course(Long id,String name,Calendar date){
super();
this.id = id;
this.name = name;
this.date = date;
}
@Override
public String toString(){
returnCourse [id =+ id +,name =+ name +,examDate =+ date
+];
}
@Id
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Calendar getDate(){
return date;
}
public void setDate(Calendar date){
this.date = date;
(非Javadoc)
* @see java.lang.Object#hashCode()
* /
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result +((id == null)?0:id.hashCode());
返回结果;
(非Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
* /
@Override
public boolean equals(Object obj){
if(this == obj){
return true;
}
if(obj == null){
return false;
if(!(obj instanceof Course)){
return false;
}
课程其他=(课程)obj;
if(id == null){
if(other.id!= null){
return false;
}
} else if(!id.equals(other.id)){
return false;
}
返回true;
}
}
我尝试在distance.hbm.xml中将courseA和B定义为Distance的属性,但是这只是对我的例外: org.hibernate.MappingException:无法确定database.datatypes的类型。课程表:distance ...
我曾尝试将courseA和B声明为组件,其中成功但当我调用 session.load Distance.class,1L)
它返回了正确的对象,但是两个课程都是空指针。
如何定义它?
另外,我怎样才能做到这一点,但是对于来自库的复杂类(比如java.util中的某些东西)
谢谢!
更新:
我发现了我可以拥有蛋糕的方式,并在距离课程中绕过它,但对我来说有一些重要的东西需要处理:课程中必须有一个日期对象。我宁愿使用java.util.Calendar,但如果这是有问题的,有任何其他方式有一个我可以使用的日期?
再次感谢!
您可以通过以下方式实现它:
@Entity
@Table(name =distance)
public class Distance {
private课程courseA,courseB;
@Embedded
public getCourseA(){
return this.courseA;
}
@Embedded
public getCourseB(){
return this.courseB;
}
}
现在Embeddable类: p>
@Embeddable
public class Address implements Serializable {
@Transient
public Long getId(){
return id;
}
@Column
public String getName(){
return name;
}
}
@ Embaddable
类不是实体,所以它不应该有任何主键。这就是为什么你应该在 @Transient id属性上
I am trying to do the following:
public class Distance {
private Course courseA, courseB;
private int minDistance;
double cost;
private Long id;
public Distance() {
super();
}
public Distance(Course courseA, Course courseB, int minDistance, double cost) {
super();
this.courseA = courseA;
this.courseB = courseB;
this.minDistance = minDistance;
this.cost = cost;
}
@Override
public String toString() {
return "Distance [courseA=" + courseA + ", courseB=" + courseB
+ ", MinDistance=" + minDistance + ", Cost=" + cost + "]";
}
public Course getCourseA() {
return courseA;
}
public void setCourseA(Course courseA) {
this.courseA = courseA;
}
public Course getCourseB() {
return courseB;
}
public void setCourseB(Course courseB) {
this.courseB = courseB;
}
public int getMinDistance() {
return minDistance;
}
public void setMinDistance(int minDistance) {
this.minDistance = minDistance;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Where Course is another class I created:
public class Course {
private Long id;
private String name;
private Calendar date;
public Course() {
super();
}
public Course(Long id,String name, Calendar date) {
super();
this.id = id;
this.name = name;
this.date = date;
}
@Override
public String toString() {
return "Course [id=" + id + ", name=" + name + ", examDate=" + date
+ "]";
}
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Calendar getDate() {
return date;
}
public void setDate(Calendar date) {
this.date = date;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Course)) {
return false;
}
Course other = (Course) obj;
if (id == null) {
if (other.id != null) {
return false;
}
} else if (!id.equals(other.id)) {
return false;
}
return true;
}
}
I tried defining courseA and B as properties of Distance in "distance.hbm.xml" but that just yelled at me with an exception: org.hibernate.MappingException: Could not determine type for: database.datatypes.Course at table:distances...
I have tried declaring courseA and B as components, which "succeeded" but when I called session.load(Distance.class,1L)
it returned the right object, but the two courses were null pointers.
How do I define it?!
Also, how can I do the same, but for complex classes from a library (like something out of java.util)
Thanks!
UPDATE:I found the way i could have my cake and work my way around it on the Distance-Course thing, but there is something important for me to work with: Course must have a date object in it. I would rather use java.util.Calendar, but if that is problematic, any other way to have a date that i could use instead?
Thanks again!
You can achieve it the following way:
@Entity
@Table(name="distance")
public class Distance {
private Course courseA, courseB;
@Embedded
public getCourseA(){
return this.courseA;
}
@Embedded
public getCourseB(){
return this.courseB;
}
}
Now the Embeddable class:
@Embeddable
public class Address implements Serializable{
@Transient
public Long getId() {
return id;
}
@Column
public String getName() {
return name;
}
}
@Embaddable
class is not entity so it should not have any Primary key. this is why you should but @Transient
on id property
这篇关于休眠:使用复杂的类作为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!