0

I new to spring hibernate. I create one table having columns and their entity should like this

School.java

@Entity
@Table(name = "school")
public class School implements Comparable<School>{

    @Transient
    private int functionValue;

    @Id
    @GeneratedValue
    private Integer id;

    @Size(min = 3, message = "Name must be at least 3 characters!")
    @UniqueUsername(message = "School Name already exists!")
    @Column(length = 1000, unique = true)
    private String name;

    private SchoolTier schoolTier1 = new SchoolTier();
    private SchoolTier schoolTier2 = new SchoolTier();
    private SchoolTier schoolTier3 = new SchoolTier();
    //their getter/setter
    }

SchoolTier

@Entity
@Table(name = "schooltier")
public class SchoolTier {

    @Id
    @GeneratedValue
    private Integer id;

    @Id
    private Integer schoolId;

    // Pojo's
    @Size(min = 0, message = " must be at least 4 characters!")
    private String CampusImprovement;

    //getter/setter 
   }

Having a table by the name of school which having same column which define in school.java and also having the table schooltier which have same column as defined in schooltier.java

But problem is here like school table doesn't having any columns for 'SchoolTier' but in school.java I have 3 object of schooltier. Now it gives an exception like

Caused by: org.hibernate.MappingException: Could not determine type for com.scp.bid.entity.SchoolTier at table: school, for columns: [org.hibernate.mapping.Column(schoolTier1)]

Now how to resolve this issue ? Please guide me if I did something wrong. Help me

2
  • You have to define the relationship between the School and the SchoolTier class for Hibernate. Something like @OneToOne(fetch=FetchType.LAZY) @JoinColumn(name="SCHOOL_TIER_ID") private SchoolTier schoolTier1 Commented Dec 20, 2016 at 12:12
  • You also don't need the second @Id on the schoolId in SchoolTier. Commented Dec 20, 2016 at 12:13

3 Answers 3

1

Use hibernate JPA @OneToOne Mapping to map School class with SchoolTier,

School will be the parent entity as in database this will be the parent table having three foreign key of the child table that is of SchoolTier entity.

This will look like this :

@Entity
@Table(name = "school")
public class School implements Comparable<School>{

@Transient
private int functionValue;

@Id
@GeneratedValue
private Integer id;

@Size(min = 3, message = "Name must be at least 3 characters!")
@UniqueUsername(message = "School Name already exists!")
@Column(length = 1000, unique = true)
private String name;

//This will create a foreign key of SchoolTier table.
@JoinColumn(name = "school_tier_id_1")
@OneToOne
private SchoolTier schoolTier1;

//This will create a foreign key of SchoolTier table.
@JoinColumn(name = "school_tier_id_2")
@OneToOne
private SchoolTier schoolTier2 = new SchoolTier();

//This will create a foreign key of SchoolTier table.
@JoinColumn(name = "school_tier_id_3")
@OneToOne
private SchoolTier schoolTier3;
//their getter/setter
}

SchoolTier will be the child entity in your case.

@Entity
@Table(name = "schooltier")
public class SchoolTier {

@Id
@GeneratedValue
private Integer id;

// Pojo's
@Size(min = 0, message = " must be at least 4 characters!")
private String CampusImprovement;

//getter/setter 
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, So I have to create school_tier_id_1, school_tier_id_2, school_tier_id_3 these columns in school table ? Right ?
The @OneToOne is part of JPA NOT Hibernate
@KushalJain yes you have to create a table school and school_tier_id_1, school_tier_id_2, school_tier_id_3 will be foreign key in your school table.
Or you can add hibernate.hbm2ddl.auto=create-drop property in your configuration, which will create the tables for you when SessionFactory is created and drops when it is explicitly closed, but do not use this in any production environment.
1

I think Hibernate doesn't let you mix and match annotation in conjunction with field / getter. If your @Id annotation is set over a field, all your mappings should follow fields. Also you need to add relation mapping for schoolTier1.

2 Comments

can you please explain it using example or above scenario. I cannot understand the words. What should I do ?
0

I highly recommend you to use relational database for this purpose. Basicly, create two table in database, one for School and one for SchoolTier. If the number of SchoolTier that School should have constant (3 in your example), you can simply create 3 column in School table for SchoolTiers' and easily connect them with relation. This will allow you to remove schoolId from SchoolTier object. As @ZeusNet suggested, you should define your School class like this;

@Entity
@Table(name = "school")
public class School implements Comparable<School>{

@Transient
private int functionValue;

@Id
@GeneratedValue
private Integer id;

@Size(min = 3, message = "Name must be at least 3 characters!")
@UniqueUsername(message = "School Name already exists!")
@Column(length = 1000, unique = true)
private String name;

@OneToOne(fetch=FetchType.LAZY) @JoinColumn(name="SCHOOL_TIER_ID")     
private SchoolTier schoolTier1

@OneToOne(fetch=FetchType.LAZY) @JoinColumn(name="SCHOOL_TIER_ID")     
private SchoolTier schoolTier2

@OneToOne(fetch=FetchType.LAZY) @JoinColumn(name="SCHOOL_TIER_ID")     
private SchoolTier schoolTier3

//their getter/setter
}

And your SchoolTier class will like;

@Entity
@Table(name = "schooltier")
public class SchoolTier {

@Id
@GeneratedValue
private Integer id;

// Pojo's
@Size(min = 0, message = " must be at least 4 characters!")
private String CampusImprovement;

//getter/setter 
}

Please read about Relational Database in Hibernate and Spring. There are a lot of examples and sample codes around the internet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.