Object relational mapping – One to many

One to many example, one Country has many State. Lets look at the country class. In the country class, there is a set of states but no @column define because country table doesn’t need to store states. @OneToMany(mappedBy=”country”) this annotation indicate that in State class will have a Country object as it’s attributes.

 

one-to-many
[googlefont font=”Enriqueta” fontsize=”30″]TblCountry.java[/googlefont]

@Entity
@Table(name = "tbl_country")
@Configurable
public class TblCountry {

	@Id
	@Column(name = "country_code", length = 10)
        private String countryCode;

	@Column(name = "country_name", length = 100)
         private String name;

        // mappedBy is object's attribute 
	@OneToMany(mappedBy = "country")
        private Set<TblState> states;

	public String getCountryCode() {
		return countryCode;
	}

	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<TblState> getStates() {
		return states;
	}

	public void setStates(Set<TblState> states) {
		this.states = states;
	}

}

 

[googlefont font=”Enriqueta” fontsize=”30″]TblState.java[/googlefont]
In state class, @ManyToOne annotation was refer to many state are belong to one country. @JoinColumn name was refer to state database table column foreign key, and referencedColumnName was refer to country object primary key.

@Entity
@Table(name = "tbl_state")
@Configurable
@JsonIgnoreProperties({"country"})
public class TblState {

    @Id
    @Column(name = "state_code", length = 10)
    private String stateCode;

    @Column(name = "state_name", length = 100)
    private String name;

    @Valid
    @ManyToOne  
    @JoinColumn(name = "country_code", //state table column (foreign key)
    referencedColumnName = "country_code") // country table columns
    private TblCountry country;

    public String getStateCode() {
    	return stateCode;
    }

    public void setStateCode(String stateCode) {
 	this.stateCode = stateCode;
    }

    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }

    public TblCountry getCountry() {
	return country;
    }

    public void setCountry(TblCountry country) {
	this.country = country;
    }

}

 

[googlefont font=”Enriqueta” fontsize=”30″]Converter[/googlefont]
If you’re using spring mvc you may need to create the converter as well, otherwise you will get country null value when u save the states object.

 
[googlefont font=”Enriqueta” fontsize=”30″]CodeToCountry.java[/googlefont]

public class CodeToCountryConverter  implements Converter<String, TblCountry>{

	@Autowired
	private TblCountryService service;

	@Override
	public TblCountry convert(String id) {
		return service.findTblCountry(id);
	} 
}

 

[googlefont font=”Enriqueta” fontsize=”30″]CountryToCode.java[/googlefont]

public class CountryToCodeConverter implements Converter<TblCountry, String>{

	@Override
	public String convert(TblCountry entity) {
		return entity.getCountryCode();
	} 

}

 

[googlefont font=”Enriqueta” fontsize=”30″]applicationContext.xml[/googlefont]

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
  <property name="converters">
      <set>
  	<bean class="com.eweol.data.converter.CountryToCodeConverter"/> 
  	<bean class="com.eweol.data.converter.CodeToCountryConverter"/>
      </set>
      </property>
  	<property name="formatters">
           <set>
  	     <bean class="com.eweol.data.converter.StringToCalendarFormatter" />
  	   </set>
  	</property>
 </bean>
Object relational mapping – One to many

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.