- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 2 个表
1.Org[ORG_ID,PARENT_ID,LANG_ID(REFERENCES LANG)]2.语言[LANG_ID,LANG_NAME]
我的 JAVA 实体文件
语言.java
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "langId")
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
@SuppressWarnings("serial")
public class Lang implements java.io.Serializable {
private BigDecimal langId;
private String langName;
private Set<Org> orgs = new HashSet<Org>(0);
public Lang() {
}
public Lang(BigDecimal langId, Set<Org> orgs) {
this.langId = langId;
this.orgs = orgs;
}
public BigDecimal getLangId() {
return this.langId;
}
public void setLangId(BigDecimal langId) {
this.langId = langId;
}
public String getLangName() {
return this.langName;
}
public void setLangName(String langName) {
this.langName = langName;
}public void setOrgs(Set<Org> orgs) {
this.orgs = orgs;
}
}
Org.java
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "orgId")
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
@SuppressWarnings("serial")
public class Org implements java.io.Serializable {
@Id
@Column(name = "orgId")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigDecimal orgId;
private BigDecimal parentOrgId;
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "langId",scope=Lang.class)
public Org() {
}
public Org(BigDecimal orgId, BigDecimal parentOrgId, Lang lang) {
this.orgId = orgId;
this.parentOrgId = parentOrgId;
this.lang = lang;
}
public BigDecimal getOrgId() {
return this.orgId;
}
public void setOrgId(BigDecimal orgId) {
this.orgId = orgId;
}
public BigDecimal getParentOrgId() {
return this.parentOrgId;
}
public void setParentOrgId(BigDecimal parentOrgId) {
this.parentOrgId = parentOrgId;
}
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public Lang getLang() {
return this.lang;
}
public void setLang(Lang lang) {
this.lang = lang;
}
}
我的 HBM Xml 文件
Org.hbm.xml
<hibernate-mapping package="com.test.entity">
<class name="com.test.entity.Org" table="ORG">
<id name="orgId" type="big_decimal">
<column name="ORG_ID" precision="22" scale="0" />
<generator class="increment" />
</id>
<property name="parentOrgId" type="big_decimal">
<column name="PARENT_ORG_ID" precision="22" scale="0" not-null="true" />
</property>
<many-to-one name="lang" class="com.test.entity.Lang" fetch="select">
<column name="LANG_ID" precision="22" scale="0" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
Lang.hbm.xml
<hibernate-mapping package="com.test.entity">
<class name="com.test.entity.Lang" table="LANG">
<id name="langId" type="big_decimal">
<column name="LANG_ID" precision="22" scale="0" />
<generator class="increment" />
</id>
<property name="langName" type="string">
<column name="LANG_NAME" length="32" not-null="true" unique="true" />
</property>
<set name="orgs" table="ORG" inverse="true" lazy="true" fetch="select">
<key>
<column name="LANG_ID" precision="22" scale="0" not-null="true" />
</key>
<one-to-many class="com.test.entity.Org" />
</set>
</class>
</hibernate-mapping>
我的 Hibernate 查询是:"FROM Org as org WHERE org.parentOrgId = :parentOrgId or org.orgId=:parentOrgId"
当我从 Controller 运行时 List listOrganization = organizationService.getAllChildWithParetOrgs(parentId);//parentId = 0
我得到结果 ObjectList/JSON 结果是
[
{
"orgId": 1,
"parentOrgId": 0,
"lang": {
"langId": 1,
"langName": "EN",
"orgs": [
{
"orgId": 2,
"parentOrgId": 1,
"lang": 1,
"currency": {
"currencyId": 1,
"currencyCode": "INR",
"currencyName": "INR",
"currencyDescription": "INDIAN RUPEE",
"orgs": [
2,
1,
{
"orgId": 3,
"parentOrgId": 1,
"lang": 1,
"currency": 1,
}
]
}
},
1,
3
]
},
"currency": 1
},
2, //---> Here No 2nd Organization Data
3 //---> Here No 3rd Organization Data
]
我无法在数组中获取第二和第三个组织数据,它在 Lang.Orgs 和 Lang.Orgs.Currency 中可用
请帮忙解决这个问题。谢谢。
最佳答案
这会让你入门,我正在使用 Spring JPA2 与 Hibernate 和 Jackson。
我让 Hibernate 从实体创建我的表。
Spring 应用程序上下文如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:component-scan base-package="com.greg" />
<tx:annotation-driven />
<jpa:repositories base-package="com.greg.repository" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.greg.entity" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
Lang 实体看起来像这样:
@Entity
public class Lang {
@Id
@Column(name = "lang_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long langId;
@Column(name = "lang_name")
private String langName;
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "lang_id", referencedColumnName = "lang_id")
private Set<Org> orgs;
组织实体如下所示:
@Entity
public class Org {
@Id
@Column(name = "org_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long orgId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "lang_id", referencedColumnName = "lang_id", updatable = false, insertable = false, nullable = false)
private Lang lang;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "parent_id", referencedColumnName = "org_id", updatable = false, insertable = false, nullable = false)
private Org parent;
这会在您的问题中创建表格。
Spring JPA 存储库很简单:
@Repository
@Transactional
public interface OrgRepository extends CrudRepository<Org, Long> {
}
和
@Repository
@Transactional
public interface OrgRepository extends CrudRepository<Lang, Long> {
}
如果我让 Jackson 连载我得到:
{
"orgId": 4,
"lang": {
"langId": 1,
"langName": "EN",
"orgs": null
},
"parent": {
"orgId": 3,
"lang": {
"langId": 1,
"langName": "EN",
"orgs": null
},
"parent": {
"orgId": 2,
"lang": {
"langId": 1,
"langName": "EN",
"orgs": null
},
"parent": null
}
}
}
我的测试是:
@Test
@Transactional
public void test1() {
try {
Lang lang = makeLang("EN");
langRepository.save(lang);
assertNotNull(lang.getLangId());
Org org1 = new Org();
org1.setLang(lang);
orgRepository.save(org1);
Org org2 = new Org();
org2.setLang(lang);
org2.setParent(org1);
orgRepository.save(org2);
Org org3 = new Org();
org3.setLang(lang);
org3.setParent(org2);
orgRepository.save(org3);
assertEquals("EN", orgRepository.findOne(org1.getOrgId()).getLang().getLangName());
assertNull(orgRepository.findOne(org1.getOrgId()).getParent());
assertEquals("EN", orgRepository.findOne(org2.getOrgId()).getLang().getLangName());
assertEquals(org1.getOrgId().longValue(), orgRepository.findOne(org2.getOrgId()).getParent().getOrgId().longValue());
assertEquals("EN", orgRepository.findOne(org3.getOrgId()).getLang().getLangName());
assertEquals(org2.getOrgId().longValue(), orgRepository.findOne(org3.getOrgId()).getParent().getOrgId().longValue());
System.out.println(new ObjectMapper().writeValueAsString(org3));
showTables("ORG");
showTables("LANG");
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
private Lang makeLang(String name) {
Lang lang = new Lang();
lang.setLangName(name);
return lang;
}
关于Hibernate @ManyToOne 和@OneToMany 获取数据作为列表(记录数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38875657/
我只是没有明白这一点。下面的代码是怎么回事,哪里出错了?我必须上课:资源和预订。一个资源可以有多个预留,并且关系是双向的。对我来说,一切似乎都找到了,我已经查看了一堆资源和文档 - 是的,还有很多示例
我正在寻找有关大型遗留模式映射的一些建议。 情况是这样的。假设我们有一个名为 BusinessTransaction 的类。此类有几个引用用户 ID 的字段: @Entity public class
我正在寻找有关大型遗留模式映射的一些建议。 情况是这样的。假设我们有一个名为 BusinessTransaction 的类。此类有几个引用用户 ID 的字段: @Entity public class
我上课了: @Entity @Table(name="users") public class User{ private Integer id; private String name; priva
我有一个实体类 付款 其中有实体类 付款方式 与 多对一 关系。 现在因为我的 PaymentMethod 是主表。 所以我想在不影响主表 PaymentMethod 的情况下保存付款。 但是每次我保
关系的 @OneToMany 端填充良好,但 @ManyToOne 端每次都会覆盖(仅最后一项保留) @Entity @Table(name="order") public c
下面的联接保留了与每个客户端相关的 TaxJurisdictions,但它不包括插入中联接 (CLIENT_CODE) 中使用的列,导致我的数据库表 TBL_TAX_JURISDICTION.CLIE
我得到了城市和天气。天气应该指向数据库中的城市对象。一个城市可以有多个天气条目。我的问题是,每次我向数据库添加天气时,它都会创建一个具有相同名称但具有其他 ID 的新城市。 天气实体; @Entity
所以我的数据库中有一份文档有修订。文档表包含文档通用的元数据,修订版包含内容。目前我可以获得正确修订的文档。 我想做的是通过映射的所有修订获取文档列表。 这是我的两个实体: @Entity publi
大家好。 我有一个问题让我睡不着)例如,如果我有一个名为 Product 的实体(类)。我应该将这个产品与一堆图像链接起来。在数据库(在我的例子中是MySQL)中,我会创建一个产品表和图像表。图像表将
我使用 Spring Roo 对 MySQL 数据库进行逆向工程,这是一个我只有读取权限的数据库。 其中一个名为 Transaction 的实体有一个名为 originalTransaction 的字
我可以建立如下关系吗: @Entity Table1{ @ManyToOne @JoinColumn(name = "Column1", referen
我有以下单向 ManyToOne 关系: @Entity @Table(name = "Child") public class Child { @Id private Integer
我的关系非常简单,我的模型如下所示: public class Project { [PrimaryKey, AutoIncrement] public int ID { get; s
我有以下数据库结构: CREATE TABLE `author` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255
目录 jpa实体@ManyToOne @OneToMany无限递归 问题描述 解决办法 @OneToMany和@ManyTo
我在保存具有 ManyToOne 双向关系的实体时遇到问题,这里是: 警告:HHH000437:正在尝试保存一个或多个与未保存的 transient 实体具有不可空关联的实体。在保存这些依赖实体之前,
我有 2 个实体 - 具有映射 @ManyToOne 的用户和角色。我想更改用户角色,但角色也想更新。 用户实体: @ManyToOne @JoinColumn(name = "role_id", i
我有 4 个类如下... @MappedSuperclass @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public abstrac
想象一下下面的类 @Embeddable class A { @ManyToOne public B classB; ... public State someEnum
我是一名优秀的程序员,十分优秀!