简体   繁体   English

休眠中的COMPOSITE-ID

[英]COMPOSITE-ID in hibernate

from

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html https://docs.jboss.org/hibernate/orm/3.3/reference/zh/html/mapping.html

i don't get how to use "composite-id" tag without "class" parameter, and those few example that i manage to Google making things even more messy. 我不知道如何在没有“ class”参数的情况下使用“ composite-id”标记,以及我设法使Google变得更加混乱的那几个例子。

so my example 所以我的例子

<class name="mainPack.Point" table="POINT">
    <composite-id>
        <key-property name="x" type="int">
            <column name="X" />
        </key-property>
        <key-property name="y" type="int">
            <column name="Y" />
        </key-property>
    </composite-id>
    <property name="str" type="java.lang.String">
        <column name="STR" />
    </property>
</class>

Will it work? 能行吗

Will columns 将列

 <column name="X" />
 <column name="Y" /> 

be present in the table? 出现在桌子上吗?

And will there be created another mapping table containing new "id class", with two parameter "X", "Y"? 并且将创建另一个包含新的“ id class”,两个参数“ X”,“ Y”的映射表吗?

  1. Will it work? 能行吗

Yes. 是。 I used hibernate-update to create a table, here is result: 我用hibernate-update创建了一个表,结果如下:

CREATE TABLE point
(
  x integer NOT NULL,
  y integer NOT NULL,
  str character varying(255),
  CONSTRAINT point_pkey PRIMARY KEY (x, y)
)

  1. Will columns be present in the table? 表格中是否会出现列?

Yes. 是。 You can see it above. 您可以在上方看到它。

  1. And will there be created another mapping table containing new "id class", with two parameter "X", "Y"? 并且将创建另一个包含新的“ id class”,两个参数“ X”,“ Y”的映射表吗?

No. 没有。


Here is java class: 这是java类:

 public class Point implements Serializable { private int x; private int y; private String str; public int getX() { return this.x; } public void setX(int x) { this.x = x; } public int getY() { return this.y; } public void setY(int y) { this.y = y; } public String getStr() { return this.str; } public void setStr(String str) { this.str = str; } @Override public int hashCode() { return new HashCodeBuilder().append(this.x).append(this.y).toHashCode(); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Point)) { return false; } Point other = (Point) obj; return (this.getX() == other.getX()) && (this.getY() == other.getY()); } 

} }

// Composite primary key bean

import java.io.Serializable;

public class CoursePk implements Serializable {

private static final long serialVersionUID = 1L;
private String title;
private String tutor;

public CoursePk(){

}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getTutor() {
    return tutor;
}
public void setTutor(String tutor) {
    this.tutor = tutor;
}
}


// Course class for composite primary key
@Entity
@Table(name="course")
public class Course implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId                    // composite key
private CoursePk id=null;

private int totalStudents;

public int getTotalStudents() {
    return totalStudents;
}

public void setTotalStudents(int totalStudents) {
    this.totalStudents = totalStudents;
}

public CoursePk getId() {
    return id;
}
public void setId(CoursePk id) {
    this.id = id;
}
}


 // Hibernate Util to get Session factory
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private HibernateUtil(){

}
 private static SessionFactory sessionFactory ;
 static {
    Configuration configuration = new Configuration();

    configuration.addAnnotatedClass (Course.class);
    configuration.setProperty("connection.driver_class","com.mysql.jdbc.Driver");
    configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");                                
    configuration.setProperty("hibernate.connection.username", "root");     
    configuration.setProperty("hibernate.connection.password", "root");
    configuration.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
    configuration.setProperty("hibernate.hbm2ddl.auto", "update");
    configuration.setProperty("hibernate.show_sql", "true");
    configuration.setProperty(" hibernate.connection.pool_size", "10");

    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
    sessionFactory = configuration.buildSessionFactory(builder.build());
 }
public  SessionFactory getSessionFactory() {
    return sessionFactory;
}

public static HibernateUtil getInstance(){
    return new HibernateUtil();
}
} 

// Main Class for testing composite primary key
public class CompositePrimaryKeyTest {
public static void main(String[] args) {
    CoursePk pk=new CoursePk();
    pk.setTitle("Java Book");
    pk.setTutor("Deepak");

    Course course=new Course();
    course.setTotalStudents(100);
    course.setId(pk);
    Session   session=HibernateUtil.getInstance().getSessionFactory().openSession();
    session.beginTransaction();

    session.save(course);

    session.getTransaction().commit();
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM